Ruby on Rails development from scratch (Windows) (24)-Test controller

Source: Internet
Author: User
Tags require ruby on rails

The previous essay introduces rails ' conventions on functional testing. This time we'll go on to the controller test.

Before we test is login, you can meet, users in login will start shopping after the action, so we now to test Store_controller, we first test the index method.

1. In index, we list all the books that can be sold, so here we have to let Store_controller use the data from Product.yml and ORDERS.YML. Now look at the store_controller_test.rb file, complete as follows:

Require File.dirname (__file__) + '/... /test_helper '
require ' Store_controller '
# re-raise errors caught by the Controller.class Storecontroller; def Rescue_action (e) Raise e end; End
class Storecontrollertest < test::unit::testcase
fixtures:p roducts,: Orders
def setup
@ Controller = storecontroller.new
@request = actioncontroller::testrequest.new
@response = Actioncontroller:: Testresponse.new
End
# Replace The Your real tests.def teardown
lineitem.delete_all
End End

Note the Teardown method here, adding this method because some of the tests we're about to write will indirectly store some lineitem in the database. By defining this teardown method after all the test methods, it is convenient to delete the test data after the test is executed, so that no other tests will be affected. After the Lineitem.delete_all is invoked, all data in the Line_item table is deleted. Normally, we don't need to do this because fixture will erase the data for us, but here we don't use line_item fixture, so we have to do it ourselves.

2. Next we add a Test_index method:

def test_index
get:index
assert_response:success
assert_equal 2, assigns (:p roducts). Size
Assert_ Template "Store/index" End

Because we have tested the crud of products in the previous model test, so here we test the index action and see the number of products, whether we use the specified view to paint (render) the page.

We used model in the controller, if the controller test failed, and model test passed, then generally to find the problem in the controller, if the controller and model tests have failed, Then we'd better look up the problem in model.

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.