SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 110 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

As the next section talks about, you should also test that the
validations you??™ve written say what you think they say.
Unit Testing Products
As mentioned in the section about testing in Chapter 3, unit tests are used to test models. I??™ll
show a few tests for ProductType and Product here, but as mentioned earlier there won??™t be
space enough to test everything as it should be tested. First of all, there is only one interesting
fact to test about ProductType: that name must be provided. So, open up the file
test/unit/product_type_test.rb, remove the test_truth method, and add this method
instead:
def test_invalid_name
p = ProductType.new
assert !p.valid?
assert p.errors.invalid?(:name)
end
This method first creates a new ProductType, and because you don??™t specify a name, it
shouldn??™t be valid. The method assert, and all methods beginning with assert_, are used to
check a certain invariant. In this method, you just check that the model object is not valid,
and that the errors provided include at least one for name.
There are more things to test for Product, though. First of all, go ahead and open the file
test/unit/product_test.rb and remove the test_truth method. The next step demands a
slight deviation into the territory of fixtures. A fixture is a YAML file that contains data for test
fixtures, which means your tests will always use the same data, instead of relying on whatever
can be found in the database at the moment. Because Products needs product types to work,
you??™ll first change those fixtures.


Pages:
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122