13.3 Playtime 13.1 Iteration H1: Email Notifications
Create an integration test
rails generate integration_test user_stories
invoke test_unit
create test/integration/user_stories_test.rb
edit test/integration/user_stories_test.rb
require 'test_helper'
class UserStoriesTest < ActionDispatch::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
LineItem.delete_all
Order.delete_all
ruby_book = products(:ruby)
get "/"
assert_response :success
assert_template "index"
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
get "/orders/new"
assert_response :success
assert_template "new"
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
orders = Order.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
mail = ActionMailer::Base.deliveries.last
assert_equal ["dave@example.com"], mail.to
assert_equal 'Sam Ruby <depot@example.com>', mail[:from].value
assert_equal "Pragmatic Store Order Confirmation", mail.subject
end
end
Run the tests
rake test:integration
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
.
Finished in 0.598739 seconds.
1 tests, 20 assertions, 0 failures, 0 errors
Create an integration test using a DSL
rails generate integration_test user_stories
invoke test_unit
conflict test/integration/user_stories_test.rb
/home/rubys/git/rails/activesupport/lib/active_support/whiny_nil.rb:48:in `method_missing': undefined method `strip' for nil:NilClass (NoMethodError)
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/shell/basic.rb:42:in `ask'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/shell/basic.rb:176:in `file_collision'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/create_file.rb:100:in `force_on_collision?'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/create_file.rb:93:in `force_or_skip_or_conflict'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/create_file.rb:77:in `on_conflict_behavior'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/empty_directory.rb:111:in `invoke_with_conflict_check'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/create_file.rb:61:in `invoke!'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions.rb:95:in `action'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/create_file.rb:26:in `create_file'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/actions/file_manipulation.rb:110:in `template'
from /home/rubys/git/rails/railties/lib/rails/generators/test_unit/integration/integration_generator.rb:9:in `create_test_files'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/task.rb:22:in `send'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:242:in `map'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/core_ext/ordered_hash.rb:75:in `each'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `map'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/group.rb:226:in `dispatch'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:109:in `send'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:109:in `invoke'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/group.rb:269:in `_invoke_for_class_method'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/shell.rb:74:in `with_padding'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/group.rb:258:in `_invoke_for_class_method'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/group.rb:134:in `_invoke_from_option_integration_tool'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/task.rb:22:in `send'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/shell.rb:14:in `map'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/core_ext/ordered_hash.rb:75:in `each'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `map'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/group.rb:226:in `dispatch'
from /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib/thor/base.rb:389:in `start'
from /home/rubys/git/rails/railties/lib/rails/generators.rb:163:in `invoke'
from /home/rubys/git/rails/railties/lib/rails/commands/generate.rb:10
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:242:in `require'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:242:in `require'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:225:in `load_dependency'
Overwrite /home/rubys/git/awdwr/edition4/work-30/depot/test/integration/user_stories_test.rb? (enter "h" for help) [Ynaqdh]
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:597:in `new_constants_in'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:225:in `load_dependency'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:242:in `require'
from /home/rubys/git/rails/railties/lib/rails/commands.rb:17
from script/rails:6:in `require'
from script/rails:6
edit test/integration/dsl_user_stories_test.rb
require 'test_helper'
class DslUserStoriesTest < ActionController::IntegrationTest
fixtures :products
DAVES_DETAILS = {
:name => "Dave Thomas",
:address => "123 The Street",
:email => "dave@example.com",
:pay_type => "Check"
}
MIKES_DETAILS = {
:name => "Mike Clark",
:address => "345 The Avenue",
:email => "mike@pragmaticstudio.com",
:pay_type => "Credit card"
}
def setup
LineItem.delete_all
Order.delete_all
@ruby_book = products(:ruby)
@rails_book = products(:two)
end
# A user goes to the store index page. They select a product,
# adding it to their cart. They then check out, filling in
# their details on the checkout form. When they submit,
# an order is created in the database containing
# their information, along with a single line item
# corresponding to the product they added to their cart.
def test_buying_a_product
dave = regular_user
dave.get "/"
dave.is_viewing "index"
dave.buys_a @ruby_book
dave.has_a_cart_containing @ruby_book
dave.checks_out DAVES_DETAILS
dave.is_viewing "index"
check_for_order DAVES_DETAILS, @ruby_book
end
def test_two_people_buying
dave = regular_user
mike = regular_user
dave.buys_a @ruby_book
mike.buys_a @rails_book
dave.has_a_cart_containing @ruby_book
dave.checks_out DAVES_DETAILS
mike.has_a_cart_containing @rails_book
check_for_order DAVES_DETAILS, @ruby_book
mike.checks_out MIKES_DETAILS
check_for_order MIKES_DETAILS, @rails_book
end
def regular_user
open_session do |user|
def user.is_viewing(page)
assert_response :success
assert_template page
end
def user.buys_a(product)
xml_http_request :post, '/line_items', :product_id => product.id
assert_response :success
end
def user.has_a_cart_containing(*products)
cart = Cart.find(session[:cart_id])
assert_equal products.size, cart.line_items.size
cart.line_items.each do |item|
assert products.include?(item.product)
end
end
def user.checks_out(details)
get "/orders/new"
assert_response :success
assert_template "new"
post_via_redirect "/orders",
:order => { :name => details[:name],
:address => details[:address],
:email => details[:email],
:pay_type => details[:pay_type]
}
assert_response :success
assert_template "index"
cart = Cart.find(session[:cart_id])
assert_equal 0, cart.line_items.size
end
end
end
def check_for_order(details, *products)
order = Order.find_by_name(details[:name])
assert_not_nil order
assert_equal details[:name], order.name
assert_equal details[:address], order.address
assert_equal details[:email], order.email
assert_equal details[:pay_type], order.pay_type
assert_equal products.size, order.line_items.size
for line_item in order.line_items
assert products.include?(line_item.product)
end
mail = ActionMailer::Base.deliveries.last
assert_equal order.email, mail[:to].value
for line_item in order.line_items
assert_operator mail.body.to_s, :include?, line_item.product.title
end
end
end
Run the tests
rake test:integration
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
...
Finished in 1.262287 seconds.
3 tests, 75 assertions, 0 failures, 0 errors