7.8. Testing Models

Testing is an integral part of developing in Rails. The subject matter is quite large and this book isn't even going to begin to do it enough justice. Nevertheless, it would do you a great disservice to close a chapter on ActiveRecord without at least scratching the bare surface of unit testing.

It is called unit testing, because individual units of code (for example, models) are tested to verify that they work correctly.

The Rails community embraces Test-Driven Development (TDD) and many developers even go as far as to write tests first, so that they act as a well-defined spec, and only then write code to pass those tests (a practice commonly known as Test-First Development).

Lately many developers have been embracing BDD (Behavior-Driven Development), often by employing the excellent RSpec. I recommend that, with the help of Google, you check out this alternative testing framework.

Many people dislike testing because they tend to prefer to spend their time writing "real code." But the reality is that, aside from being a staple of the XP methodology, testing improves the quality of your application. Testing cannot guarantee a complete lack of bugs in your Web application, but with good testing coverage you can ensure that many basic cases/functionalities are properly handled.

More importantly, it gives you confidence when it comes time to make changes and develop the application further, without having to worry that a change may break a related piece of ...

Get Ruby on Rails® for Microsoft Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.