At the Forge - Testing with Rails
Before we can begin testing, however, we need to overcome a problem: if we want to test our application, we should first populate the database tables with data. Moreover, we want it to be the same, consistent data each time we run our tests, so that we can know what is being tested. Rails solves this problem with fixtures, which automatically populate our test database before we want to test. The directory blog/test/fixtures in our system is where we can create such fixtures, generally using YAML, yet another markup language, whose structure is largely determined through indentation.
We create one YAML file for each database table we want to test. Our database contains only one table, so we have to create only a single YAML file, blogs.yml. Sure enough, when we look at the blog/test/fixtures directory, we see that there is already such a file there, demonstrating how we create our fixtures and pointing us to documentation at ar.rubyonrails.org/classes/Fixtures.html, in case we don't completely understand how fixtures work.
We now create one or more entries in our blog.yml file, each of which corresponds to a single row in the Blogs database table, corresponding in turn to a single instance of the Blog object defined in our blog/app/models directory. As a reminder, our table definition is as follows:
CREATE TABLE Blogs ( id SERIAL NOT NULL, title TEXT NOT NULL, contents TEXT NOT NULL, posted_at TIMESTAMP NOT NULL DEFAULT NOW(), PRIMARY KEY(id) );
Here is how we could create two fixtures for this table:
blog_entry_one: id: 1 title: My first entry! contents: It was a dark and stormy night, and I forgot my umbrella. So I decided to tell the world on my blog. posted_at: 2005-Sep-1 22:00:00 blog_entry_two: id: 2 title: My second entry! contents: It was much nicer this morning. posted_at: 2005-Sep-1 22:00:00
This is the equivalent of two INSERT statements. Given that INSERT is standard SQL, why would we prefer to use fixtures?
To begin with, fixtures ensure that we always start with the same baseline data. It's terribly frustrating to run tests, only to have them fail because of a uniqueness constraint that was triggered by duplicate data.
The second reason is that it allows us to test our database objects not only in the database itself, but also from a source. That is, the Rails test system loads the data in our YAML file into the database, and then accesses them via our model objects. It then loads the values from our YAML file a second time, making them available via a hash. We can then compare the two, ensuring that data was imported correctly before we begin to test more sophisticated methods.
With our fixture in place, we now can begin to execute the first of our unit tests. Unit tests check individual methods and objects. If an application's components pass a complete set of unit tests, there is still room for error. However, those errors tend to be from the integration of the units, which are covered by a different set of tests.
Not surprisingly, unit tests are defined inside of the test/unit directory within our main application directory. Rails automatically creates a unit test file for each model class you have defined; thus, my test/unit directory contains a file named blog_test.rb, corresponding to the Blog class defined in app/models/blog.rb. (Remember: Rails model objects have singular names and refer to database tables that have plural names.)
By default, the file contains a skeleton for our unit tests:
require File.dirname(__FILE__) + '/../test_helper' class BlogTest < Test::Unit::TestCase fixtures :blogs def setup @blog = Blog.find(1) end # Replace this with your real tests. def test_truth assert_kind_of Blog, @blog end end
The first line helps to bootstrap the test mechanism and is not of immediate interest. But then we see that we are defining a class (called BlogTest, using the Rails naming convention that we would expect in a file called blog_test.rb). BlogTest is a subclass of Test::Unit::TestCase, which comes with Rails, and provides us with a number of different test-related features.
The definition of BlogTest begins with a declaration, fixtures, whose value is :blogs—an indication that when Rails wants to test our Blog object with the BlogTest object, it should first populate the test database with fixtures defined in test/fixtures/blogs.yml. If we are interested in using multiple fixtures, we can name them as well:
fixtures :blogs, :foo, :bar
Two methods are defined for us by default, called setup and test_truth. The setup method, as you might expect from its name, gets things ready for our tests. In this particular case, it invokes Blog.find(), giving it a parameter of 1. In other words, it retrieves the object whose primary key is 1 (that is, blog_entry_one) and puts it into @blog. Perl programmers might need to remember that in Ruby, @blog is an instance variable, not necessarily an array. In this particular case, @blog contains a single instance of Blog—the object that Blog.find(1) returned.
The other method, test_truth, then uses one of the predefined test assertions that comes with Rails. In this particular case, we use the assert_kind_of assertion to check that @blog is an instance of Blog.
To run this initial version of our tests, we simply say:
$ ruby blog_test.rb
As the tests run, we get a status report. If all goes well, the output should look like this:
[reuven@server unit]$ ruby blog_test.rb Loaded suite blog_test Started . Finished in 19.066227 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
The first few times that I ran these tests, I received error messages. The first problem was that I had failed to change blogs.yml from its original, default state, without defining anything but the id primary key field. Because of the integrity constraints that I had put on the Blogs table, PostgreSQL stopped the test, indicating that it would not allow NULL values in the title field.
The second time I tried to run the tests, Rails picked up an error in my YAML file, reminding me that the YAML format requires consistent indentation with spaces and without any tab characters.
Rails wisely distinguishes between failures and errors; both of the above were classified as errors, letting me focus on the overall test environment rather than a particular method.
We can add additional tests by defining new methods. For example, let's check that the title of @blog matches the value we put in the YAML file. We can add the following to the BlogTest class definition:
def test_title assert_equal @blogs["blog_entry_one"]["title"], @blog.title end
Notice how our test begins with the characters test_. This tells Rails that this method should be part of our test suite. Because each individual method is counted separately, it is probably best to have a large number of test methods, each of which contains a small number of assertions. There is no technical reason why you cannot put a large number of assertions in the same method, but it means you might have a tougher time understanding just where the problem lies.
In this case, we are using assert_equal to check that two quantities are equal. Pay close attention to the very similar names, and you will see what we are doing. The first item to test for equality is @blogs[“blog_entry_one”][“title”]. The @blogs hash is created automatically for us by the test suite, and (as mentioned earlier) contains the entire YAML fixture definition file. If @blogs contains the entire YAML definition, then @blogs[“blog_entry_one”] contains the first fixture, and @blogs[“blog_entry_one”][“title”] contains the title of the first one.
@blog, by contrast, has a singular name because it contains only a single instance of Blog. And like all good objects descended from ActiveRecord, we can use a method to retrieve the contents of a field—in this case, @blog.title. So, the bottom line is that this test helps us check that the title is the same.
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems
Join editor Bill Childers and Bit9's Paul Riegle on April 27 at 12pm Central to learn how to keep your Linux systems secure.
Free to Linux Journal readers.Register Now!
- Considering Legacy UNIX/Linux Issues
- Cluetrain at Fifteen
- [<Megashare>] Watch Mrs Brown's Boys Movie Online Full Movie HD 2014
- New Products
- Getting Good Vibrations with Linux
- Memory Ordering in Modern Microprocessors, Part I
- Tech Tip: Really Simple HTTP Server with Python
- RSS Feeds
- Security Hardening with Ansible
- diff -u: What's New in Kernel Development