user_stories_test.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 UserStoriesTest < ActionDispatch::IntegrationTest
  11. fixtures :products
  12. # A user goes to the index page. They select a product, adding it to their
  13. # cart, and check out, filling in their details on the checkout form. When
  14. # they submit, an order is created containing their information, along with a
  15. # single line item corresponding to the product they added to their cart.
  16. test "buying a product" do
  17. LineItem.delete_all
  18. Order.delete_all
  19. ruby_book = products(:ruby)
  20. get "/"
  21. assert_response :success
  22. assert_template "index"
  23. xml_http_request :post, '/line_items', product_id: ruby_book.id
  24. assert_response :success
  25. cart = Cart.find(session[:cart_id])
  26. assert_equal 1, cart.line_items.size
  27. assert_equal ruby_book, cart.line_items[0].product
  28. get "/orders/new"
  29. assert_response :success
  30. assert_template "new"
  31. post_via_redirect "/orders",
  32. order: { name: "Dave Thomas",
  33. address: "123 The Street",
  34. email: "dave@example.com",
  35. pay_type: "Check" }
  36. assert_response :success
  37. assert_template "index"
  38. cart = Cart.find(session[:cart_id])
  39. assert_equal 0, cart.line_items.size
  40. orders = Order.all
  41. assert_equal 1, orders.size
  42. order = orders[0]
  43. assert_equal "Dave Thomas", order.name
  44. assert_equal "123 The Street", order.address
  45. assert_equal "dave@example.com", order.email
  46. assert_equal "Check", order.pay_type
  47. assert_equal 1, order.line_items.size
  48. line_item = order.line_items[0]
  49. assert_equal ruby_book, line_item.product
  50. mail = ActionMailer::Base.deliveries.last
  51. assert_equal ["dave@example.com"], mail.to
  52. assert_equal 'Sam Ruby <depot@example.com>', mail[:from].value
  53. assert_equal "Pragmatic Store Order Confirmation", mail.subject
  54. end
  55. end