8.9. Testing Controllers

Testing is an important component of performing Agile Web development. In fact, as stressed multiple times before, one of the fundamental principles of the Agile methodologies is the ability to change the software code base to respond to changes in requirements.

It's easy enough to verify that routing and controllers are working as expected by firing up your favorite browser and trying out paths within the application. But this approach is not systematic and won't provide you with any level of confidence, as you continue to change and evolve your application. Furthermore, working on a code base that doesn't have good test coverage is risky and tends to require much more time and effort while debugging.

As a reminder, at any time you can check your test coverage and other interesting statistics about your project by running rake stats.

For these reasons, this chapter concludes with a brief tour of what Rails bakes-in for testing routes and controllers.

8.9.1. Testing Routes

Route testing consists of writing unit tests that verify that the mapping between paths and the controllers' code works as expected.

Your routing tests can be stored in test\unit\routing_test.rb, which is just going to be a regular test case (within the context of Rails):

require 'test_helper'

class RoutingTest < ActiveSupport::TestCase

 #... Your routing tests...

end

This file is not generated automatically by Rails and must be manually created by developers who intend to test routes. ...

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.