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.