Ruby on Rails development from scratch (Windows) (19)-Test start

Source: Internet
Author: User
Tags command line ruby on rails

We've already completed a simple shopping cart, and from this start we'll see how to test in rails.

When we created the shopping cart program in our depot directory, there is already a test directory, which is for us to test preparation. So far, we've seen that the fixtrues and Functional,unit directories inside have a test file that corresponds to the controller and model.

We first Test products this model. Code test\unit the product_test.rb file in the directory, modify its contents as follows:

Require File.dirname (__file__) + '/... /test_helper '
class Producttest < test::unit::testcase
fixtures:p roducts
def setup
@ Product = Product.find (1)
End
# Replace this with your real tests.
def Test_truth
assert true
E nd End

Then run the test command on the command line: \rails_apps\depot>ruby test/unit/product_test.rb, you will see the following output:

Loaded Suite test/unit/product_test
started
E
finished in 0.312 seconds.
1) Error:
Test_truth (producttest):
activerecord::statementinvalid:mysql::error:table ' depot_ Test.products ' doesn ' t exist:delete
from the products ... 1 tests, 0 assertions, 0 failures, 1 errors  

As you can see from the above information, there is no products this table in the Depot_test database. That's because, when we created a rails project, we created three libraries in MySQL: development,test,production, we used to write code using development libraries, and now we're testing Rails uses the test library. What we're going to do now is create a Products table in the test library, you can use SQL statements to do table creation, but rails gives us a more convenient way to use the rake command at the command line:

Depot>rake Clone_structure_to_test

This will development the library structure to the test library, and you'll see that there are four tables in the test library that we've used.

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.