tailieunhanh - Agile Web Development with Rails phần 4

Trên dòng 3, chúng tôi tạo ra một đối tượng đặt hàng mới và khởi tạo từ các dữ liệu mẫu. Trong trường hợp này, chúng tôi muốn tất cả các dữ liệu mẫu liên quan đến đối tượng thứ tự, vì vậy chúng tôi lựa chọn: băm để từ các thông số (chúng tôi sẽ nói về hình thức là liên kết với các mô hình trên trang 341). tiếp theo | Testing Controllers 156 redirect_to_url The full URL that the previous action redirected to. assert_equal http login redirect_to_url We ll see more of these assertions and variables in action as we write more tests so let s get back to it. Buy Something Already The next feature we d be wise to test is that a user can actually place an order for a product. That means switching our perspective over to the storefront. We ll walk through each action one step at a time. Listing Products for Sale Back in the Storecontroller the index action puts all the salable products into the products instance variable. It then renders the view which uses the products variable to list all the products for sale. To write a test for the index action we need some products. Thankfully we already have two salable products in our products fixture. We just need to modify the file to load the products fixture. While we re at it we load the orders fixture which contains one order that we ll need a bit later. File119 require FILE__ . test_helper require store_controller Reraise errors caught by the controller. class StoreController def rescue_action e raise e end end class StoreControllerTest Test Unit TestCase fixtures products orders def setup @controller @request ActionController @response ActionController end def teardown end end Notice that we ve added a new method called teardown to this test case. We do this because some of the test methods we ll be writing will indirectly cause line items to be saved in the test database. If defined the teardown method is called after every test method. This is a handy way to clean up the test database so that the results of one test method don t affect another. By calling in teardown the line_items table in the test database will be cleared after each test method runs. If we re Report erratum .

TỪ KHÓA LIÊN QUAN