dsl_user_stories_test.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. require 'test_helper'
  10. class DslUserStoriesTest < ActionDispatch::IntegrationTest
  11. fixtures :products
  12. DAVES_DETAILS = {
  13. :name => "Dave Thomas",
  14. :address => "123 The Street",
  15. :email => "dave@example.com",
  16. :pay_type => "Check"
  17. }
  18. MIKES_DETAILS = {
  19. :name => "Mike Clark",
  20. :address => "345 The Avenue",
  21. :email => "mike@pragmaticstudio.com",
  22. :pay_type => "Credit card"
  23. }
  24. def setup
  25. LineItem.delete_all
  26. Order.delete_all
  27. @ruby_book = products(:ruby)
  28. @rails_book = products(:two)
  29. end
  30. # A user goes to the store index page. They select a product,
  31. # adding it to their cart. They then check out, filling in
  32. # their details on the checkout form. When they submit,
  33. # an order is created in the database containing
  34. # their information, along with a single line item
  35. # corresponding to the product they added to their cart.
  36. def test_buying_a_product
  37. dave = regular_user
  38. dave.get "/"
  39. dave.is_viewing "index"
  40. dave.buys_a @ruby_book
  41. dave.has_a_cart_containing @ruby_book
  42. dave.checks_out DAVES_DETAILS
  43. dave.is_viewing "index"
  44. check_for_order DAVES_DETAILS, @ruby_book
  45. end
  46. def test_two_people_buying
  47. dave = regular_user
  48. mike = regular_user
  49. dave.buys_a @ruby_book
  50. mike.buys_a @rails_book
  51. dave.has_a_cart_containing @ruby_book
  52. dave.checks_out DAVES_DETAILS
  53. mike.has_a_cart_containing @rails_book
  54. check_for_order DAVES_DETAILS, @ruby_book
  55. mike.checks_out MIKES_DETAILS
  56. check_for_order MIKES_DETAILS, @rails_book
  57. end
  58. def regular_user
  59. open_session do |user|
  60. def user.is_viewing(page)
  61. assert_response :success
  62. assert_template page
  63. end
  64. def user.buys_a(product)
  65. xml_http_request :post, '/line_items', :product_id => product.id
  66. assert_response :success
  67. end
  68. def user.has_a_cart_containing(*products)
  69. cart = Cart.find(session[:cart_id])
  70. assert_equal products.size, cart.line_items.size
  71. cart.line_items.each do |item|
  72. assert products.include?(item.product)
  73. end
  74. end
  75. def user.checks_out(details)
  76. get "/orders/new"
  77. assert_response :success
  78. assert_template "new"
  79. post_via_redirect "/orders",
  80. :order => { :name => details[:name],
  81. :address => details[:address],
  82. :email => details[:email],
  83. :pay_type => details[:pay_type]
  84. }
  85. assert_response :success
  86. assert_template "index"
  87. cart = Cart.find(session[:cart_id])
  88. assert_equal 0, cart.line_items.size
  89. end
  90. end
  91. end
  92. def check_for_order(details, *products)
  93. order = Order.find_by_name(details[:name])
  94. assert_not_nil order
  95. assert_equal details[:name], order.name
  96. assert_equal details[:address], order.address
  97. assert_equal details[:email], order.email
  98. assert_equal details[:pay_type], order.pay_type
  99. assert_equal products.size, order.line_items.size
  100. for line_item in order.line_items
  101. assert products.include?(line_item.product)
  102. end
  103. mail = ActionMailer::Base.deliveries.last
  104. assert_equal order.email, mail[:to].value
  105. for line_item in order.line_items
  106. assert_operator mail.body.to_s, :include?, line_item.product.title
  107. end
  108. end
  109. end