Archive for February, 2008

Testing in Rails: Part 11 - Running Unit Tests

Wednesday, February 27th, 2008

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.

One final post about unit tests and then we will move on to functional testing.

Running All Tests in a Single Test File

Up to now we have run our unit tests by simply asking Ruby to process the file that contains our tests.

>ruby test/unit/wine_test.rb

When you are first developing unit tests on a single model, this may be the easiest way to do it. It will run all the tests in the WineTest file.

(more…)

Testing in Rails: Part 10 - Assertions

Wednesday, February 20th, 2008

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.

I dreamed I was assertive, image copyright Celia Perez
© Celia Perez

At this point in the series, we have covered most aspects of unit testing in Ruby on Rails. We learned the basics both in Ruby and in Rails and discussed how to write meaningful tests. We set up fixtures to make working with sample data easier. We learned to write tests for ActiveRecord objects and their relationships, validations, additional attributes and callbacks.

The only thing we have not done is write tests for the custom methods in our Winery and Wine models (such as: location, find_newest, age, is_antique?). But I feel confident that you have the testing skills to make that an easy task.

To ensure that you have everything you need for writing unit tests on your own, in this section I want to expand on the assertions you can utilize for unit testing.

(more…)

Testing in Rails: Part 9 - Attributes and Callbacks

Thursday, February 7th, 2008

Wine Rack

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.

In previous sections, we learned to unit test the ActiveRecord associations and validations in our classes. Both are extremely common and appear in most Rails models. While less common, attribute definition methods and callbacks are still used somewhat frequently and are worth learning to test.

Hopefully by now your knowledge and confidence about writing tests is growing!

Attribute Definition Methods

When I refer to attribute definition methods, a classification I made up, I am referring to the methods that define and describe attributes in our model. There are two kinds. First, there are methods in the Ruby language that define attribute reader and writer methods: attr, attr_accessor, attr_reader, and attr_writer. Second, there are methods in the Rails framework that limit the access to attributes: attr_accessible, attr_protected, and attr_readonly.

(more…)