dsl_user_stories_test.rb 4.3 KB

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