require 'test_helper' class UserStoriesTest < ActionController::IntegrationTest fixtures :products # A user goes to the index page. They select a product, adding it to their # cart, and check out, filling in their details on the checkout form. When # they submit, an order is created containing their information, along with a # single line item corresponding to the product they added to their cart. test "buying a product" do #START:setup LineItem.delete_all Order.delete_all ruby_book = products(:ruby) #END:setup #START:step1 get "/" assert_response :success assert_template "index" #END:step1 #START:step2 xml_http_request :post, '/line_items', :product_id => ruby_book.id assert_response :success cart = Cart.find(session[:cart_id]) assert_equal 1, cart.line_items.size assert_equal ruby_book, cart.line_items[0].product #END:step2 #START:step3 get "/orders/new" assert_response :success assert_template "new" #END:step3 #START:step4 post_via_redirect "/orders", :order => { :name => "Dave Thomas", :address => "123 The Street", :email => "dave@example.com", :pay_type => "Check" } assert_response :success assert_template "index" cart = Cart.find(session[:cart_id]) assert_equal 0, cart.line_items.size #END:step4 #START:step5 orders = Order.find(:all) assert_equal 1, orders.size order = orders[0] assert_equal "Dave Thomas", order.name assert_equal "123 The Street", order.address assert_equal "dave@example.com", order.email assert_equal "Check", order.pay_type assert_equal 1, order.line_items.size line_item = order.line_items[0] assert_equal ruby_book, line_item.product #END:step5 #START:step6 mail = ActionMailer::Base.deliveries.last assert_equal ["dave@example.com"], mail.to assert_equal 'Sam Ruby ', mail[:from].value assert_equal "Pragmatic Store Order Confirmation", mail.subject #END:step6 end end