Ruby on Rails 2.1 - Applications' Unit Testing

Introduction:

The Tests - They are test applications that produce consistent result and prove that a Rails application does what it is expected to do. Tests are developed concurrently with the actual application.

The Assertion - This is a one line of code that evaluates an object (or expression) for expected results. For example, is this value = that value? is this object nil?

The Test Case - This is a class inherited from Test::Unit::TestCase containing a testing strategy comprised of contextually related tests.

The Test Suite - This is a collection of test cases. When you run a test suite, it will, in turn, execute each test that belongs to it.

Rails Testing:

When you run the helper script script/generate to create controllers and models, Rails generate a framework for unit and functional tests. You can get pretty good test coverage by filling in the framework with tests for the functionality you write. There are two important points to test in a Rails application:

Testing the Models.

Testing the Controllers.

This tutorial will cover both the testings in brief. So let's create one testapp to understand the concept.

C:\ruby> rails -d mysql testapp

Database Setup:

Till now we have used only Rails application's development database, but now you needed to make sure that the testing database is also created and appropriate section of your config/database.yml file was set up correctly.

This will create books table in testapp_development database. Now after this we need to set up your test database using rake command as follows:

C:\ruby\testapp > rake db:test:clone_structure

This will clone testapp_development database into testapp_test database. It means whatever you have in development database, now you will have in test database as well.

Testing Models:

When you generate a model with the generate script, Rails also generates a unit test script for the model in the test directory. It also creates a fixture, a YAML file containing test data to be loaded into the testapp_test database. This is the data against which your unit tests will run:

The BookTest method starts off by creating a new Book object using the title and other fields from the first record in the text fixture/books.yml. The resulting object is stored in the perl_book instance variable.

The first assertion tests that saving the Book object was successful.

Next, the book object is retrieved using the find method and stored in another instance variable named perl_book_copy. The success of this retrieval is tested in the next assertion, which compares the titles of both book objects. At this point, we've tested the ability to create and read a database record.

The solution tests updating by assigning a new title to the object stored in perl_book and then asserts that saving the change is successful.

Finally, the ability to destroy a Book object is tested.

This is how we can test our Rails Models.

Testing Controllers:

Controller testing is also known as functional testing. Functional testing
tests the following type of functionalities of the controllers:

Is the response redirected as expected ?

Is the expected template rendered?

Is the routing as expected

Does the response contain the expected tags?

Rails framework supports 5 types of requests and to write a functional test, you need to simulate any of the five HTTP request types that your controller will process. :

get

post

put

head

delete

Request type "get" and "post" are the most commonly used in controller testing. All these methods take four arguments:

The action of a controller

An optional hash of request parameters

An optional session hash

An optional flash hash

In this tutorial, we will see how to use get method to test our controller. You can test rest of the methods in similar way.

When you generate a controller with generate, Rails creates a functional test script for the controller as follows:

As you write code in the controller classes, you'll write corresponding tests in these files. Before that let's define our controller functions list, show, and search inside app/controllers/book_controller.rb as follows: