user_stories_test.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. require 'test_helper'
  18. class UserStoriesTest < ActionDispatch::IntegrationTest
  19. fixtures :products
  20. # A user goes to the index page. They select a product, adding it to their
  21. # cart, and check out, filling in their details on the checkout form. When
  22. # they submit, an order is created containing their information, along with a
  23. # single line item corresponding to the product they added to their cart.
  24. test "buying a product" do
  25. LineItem.delete_all
  26. Order.delete_all
  27. ruby_book = products(:ruby)
  28. get "/"
  29. assert_response :success
  30. assert_template "index"
  31. xml_http_request :post, '/line_items', :product_id => ruby_book.id
  32. assert_response :success
  33. cart = Cart.find(session[:cart_id])
  34. assert_equal 1, cart.line_items.size
  35. assert_equal ruby_book, cart.line_items[0].product
  36. get "/orders/new"
  37. assert_response :success
  38. assert_template "new"
  39. post_via_redirect "/orders",
  40. :order => { :name => "Dave Thomas",
  41. :address => "123 The Street",
  42. :email => "dave@example.com",
  43. :pay_type => "Check" }
  44. assert_response :success
  45. assert_template "index"
  46. cart = Cart.find(session[:cart_id])
  47. assert_equal 0, cart.line_items.size
  48. orders = Order.all
  49. assert_equal 1, orders.size
  50. order = orders[0]
  51. assert_equal "Dave Thomas", order.name
  52. assert_equal "123 The Street", order.address
  53. assert_equal "dave@example.com", order.email
  54. assert_equal "Check", order.pay_type
  55. assert_equal 1, order.line_items.size
  56. line_item = order.line_items[0]
  57. assert_equal ruby_book, line_item.product
  58. mail = ActionMailer::Base.deliveries.last
  59. assert_equal ["dave@example.com"], mail.to
  60. assert_equal 'Sam Ruby <depot@example.com>', mail[:from].value
  61. assert_equal "Pragmatic Store Order Confirmation", mail.subject
  62. end
  63. end