Ruby on Rails development from scratch (Windows) (20)-Test model (CRUD)

Source: Internet
Author: User
Tags ruby on rails

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.