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

Source: Internet
Author: User
Tags empty ruby on rails

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

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.