Agile Web Development with Rails, Edition 4

13.3 Playtime 13.1 Iteration H1: Email Notifications

13.2 Iteration H2: Integration Tests

3 (tests|runs), 47 assertions, 0 failures, 0 errors.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:38:in `assert_test_summary'
  /home/rubys/git/awdwr/edition4/checkdepot.rb:306:in `block in <class:DepotTest>'

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
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

    
Environment data not found in the schema. To resolve this issue, run: 

    
	bin/rails db:environment:set RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:1259:in `last_stored_environment'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
rake aborted!
ActiveRecord::PendingMigrationError: 

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
/home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
/home/rubys/git/awdwr/edition4/work-230/depot/test/integration/user_stories_test.rb:1:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/integration/user_stories_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:63:in `rake_run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:32:in `block (3 levels) in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:integration
(See full trace by running task with --trace)

Create an integration test using a DSL

rails generate integration_test dsl_user_stories
      invoke  test_unit
      create    test/integration/dsl_user_stories_test.rb
edit test/integration/dsl_user_stories_test.rb
require 'test_helper'
 
class DslUserStoriesTest < ActionDispatch::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
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

    
Environment data not found in the schema. To resolve this issue, run: 

    
	bin/rails db:environment:set RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:1259:in `last_stored_environment'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
rake aborted!
ActiveRecord::PendingMigrationError: 

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
/home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
/home/rubys/git/awdwr/edition4/work-230/depot/test/integration/dsl_user_stories_test.rb:1:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/integration/dsl_user_stories_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:63:in `rake_run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:32:in `block (3 levels) in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:integration
(See full trace by running task with --trace)

13.3 Playtime 13.1 Iteration H1: Email Notifications