The essay briefly understands the use of rails ' test and test data, and this time to see how to add a test to a model for deletion and modification.
1. Or using the last written products_test.rb, modify the Test_turth method by the name Test_create and make the contents:
def test_create
assert_kind_of Product, @product
assert_equal 1, @product. ID
assert_equal "pragmatic Ver Sion control ", @product. Title
assert_equal" I to use version control ", @product. Description
assert_equal" HT Tp://.../sk_svn_small.jpg ", @product. Image_url
assert_equal 29.95, @product. Price
assert_equal" 2005-01-26 00:00:00 ",
@product. Date_available_before_type_cast
End
Then run the test command: Depot>ruby test/unit/product_test.rb, the screen will display information:
Loaded Suite test/unit/product_test
started
F
finished in 0.109 seconds.1) failure:
Test_create ( Producttest) [test/unit/product_test.rb:16]:
<29> expected but
was <#<bigdecimal:4aad7b0, ' 0.2995E2 ', 8 (8) >>.1 tests, 6 assertions, 1 failures, 0 errors
We see, it's assert_equal 29.95, @product. Price assertion failed. According to the contents of the Agile Web Development with Rails, this assertion should be passed normally. But I don't know if it's a version or an environmental problem, I don't always do it when I write it myself. In order to be able to make the assertion pass, we modify the
Assert_equal 29.95, @product. Price
Instead: Assert_equal "29.95", @product. Price_before_type_cast
As we can see, each property of the Product object has a corresponding _before_type_cast version, and its contents are a string.
Now run the test command again and get the following results:
Loaded Suite test/unit/product_test
started
. Finished in 0.078 seconds.
1 tests, 7 assertions, 0 failures, 0 errors
As you can see from the test above, in the Setup method, we looked up a record with ID 1 from the database, and then judged the properties of the Test_create method individually.