This time we continue to test controller, or Store_controller.
1. Test the checkout function first to add code to STORE_CONTROLLER_TEST.RB:
def test_checkout
Test_add_to_cart
get:checkout
assert_response:success
Assert_not_nil assigns ( : order)
assert_template the "store/checkout" End
Note that another test method Test_add_to_cart is called, because if the cart is empty, we will not be able to go to the desired checkout page, so we have to have at least one item in the cart. So instead of writing duplicate code, we called the Test_add_to_cart method to make the shopping cart a commodity.
2. Again to test the Save order function, we will save orders through the action of Save_order, our method is: Cart store items into the model order, and then saved to the database, and then the cart empty, Reposition to the shopping store page and display a friendly message. Now add code to STORE_CONTROLLER_TEST.RB:
def test_save_invalid_order
Test_add_to_cart
Post:save_order,: Order => {: Name => ' Fred ',: Email => Nil}
assert_response:success
assert_template "store/checkout"
assert_tag:tag => "div": Attributes => {: Class => "Fieldwitherrors"}
assert_equal 1, session[:cart].items.size
end
The above is to test invalid order, we pass to Save_order action an invalid order, when invalid order is submitted, we use Assert_tag to check the response of the DIV node is fieldwitherrors type.
3. We will then test the preservation of a valid order.
def test_save_valid_order
Test_add_to_cart
assert_equal 1, session[:cart].items.size
assert_equal 1, Order.count
post:save_order: Order => @valid_order_for_fred. Attributes
assert_redirected_to:action = > ' index '
assert_equal ' Thank for your order. ", Flash[:notice]
follow_redirect assert_template
" Store/index "
assert_equal 0, Session[:cart].items.size
assert_equal 2, Order.find_all.size
end