Agile Web Development with Rails, Edition 4

12.1 Iteration G1: Capturing an Order 11.5 Iteration F5: Making Images Clickable

11.6 Iteration F6: Testing AJAX changes

RuntimeError: Edit test/controllers/line_items_controller_test.rb failed at makedepot.rb:1801

Traceback:
  

Verify that yes, indeed, the product index is broken.

get /products

NoMethodError in Products#index

Showing /home/rubys/git/awdwr/edition4/work-230/depot/app/views/layouts/application.html.erb where line #21 raised:

undefined method `line_items' for nil:NilClass
Extracted source (around line #21):
19
20
21
22
23
24
              
<!-- START_HIGHLIGHT -->
<!-- START:hidden_div -->
<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
<%= render @cart %>
<% end %>
<!-- END:hidden_div -->

Rails.root: /home/rubys/git/awdwr/edition4/work-230/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Response

Headers:

None

Conditionally display the cart.

edit app/views/layouts/application.html.erb
      <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
          <%= render @cart %>
        <% end %>
      <% end %>

Update the redirect test.

edit test/controllers/line_items_controller_test.rb
#<IndexError: regexp not matched>
  /home/rubys/git/gorp/lib/gorp/edit.rb:150:in `[]='
  /home/rubys/git/gorp/lib/gorp/edit.rb:150:in `msub'
  makedepot.rb:1801:in `block (3 levels) in <main>'
  /home/rubys/git/gorp/lib/gorp/edit.rb:95:in `instance_exec'
  /home/rubys/git/gorp/lib/gorp/edit.rb:95:in `block in edit'
  /home/rubys/git/gorp/lib/gorp/edit.rb:93:in `sub!'
  /home/rubys/git/gorp/lib/gorp/edit.rb:93:in `edit'
  makedepot.rb:1800:in `block (2 levels) in <main>'
  /home/rubys/git/gorp/lib/gorp/edit.rb:173:in `instance_exec'
  /home/rubys/git/gorp/lib/gorp/edit.rb:173:in `edit'
  makedepot.rb:1798:in `block in <main>'
  /home/rubys/git/gorp/lib/gorp/output.rb:59:in `block (4 levels) in <top (required)>'
  /home/rubys/git/gorp/lib/gorp/output.rb:49:in `each'
  /home/rubys/git/gorp/lib/gorp/output.rb:49:in `block (3 levels) in <top (required)>'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in `_nested_structures'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in `tag!'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:93:in `method_missing'
  /home/rubys/git/gorp/lib/gorp/output.rb:22:in `block (2 levels) in <top (required)>'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in `_nested_structures'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in `tag!'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:93:in `method_missing'
  /home/rubys/git/gorp/lib/gorp/output.rb:11:in `block in <top (required)>'
    
require 'test_helper'
 
class LineItemsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @line_item = line_items(:one)
  end
 
  test "should get index" do
    get line_items_url
    assert_response :success
  end
 
  test "should get new" do
    get new_line_item_url
    assert_response :success
  end
 
  test "should create line_item" do
    assert_difference('LineItem.count') do
      post line_items_url, params: { line_item: { cart_id: @line_item.cart_id, product_id: @line_item.product_id } }
    end
 
    assert_redirected_to line_item_path(LineItem.last)
  end
 
  test "should show line_item" do
    get line_item_url(@line_item)
    assert_response :success
  end
 
  test "should get edit" do
    get edit_line_item_url(@line_item)
    assert_response :success
  end
 
  test "should update line_item" do
    patch line_item_url(@line_item), params: { line_item: { product_id: @line_item.product_id } }
    assert_redirected_to line_item_path(@line_item)
  end
 
  test "should destroy line_item" do
    assert_difference('LineItem.count', -1) do
      delete line_item_url(@line_item)
    end
 
    assert_redirected_to line_items_path
  end
end

Add an XHR test.

edit test/controllers/line_items_controller_test.rb
  test "should create line_item via ajax" do
    assert_difference('LineItem.count') do
      xhr :post, :create, product_id: products(:ruby).id
    end 
 
    assert_response :success
    assert_select_jquery :html, '#cart' do
      assert_select 'tr#current_item td', /Programming Ruby 1.9/
    end
  end

Add an test in support for the coffeescript changes.

edit test/controllers/store_controller_test.rb
  test "markup needed for store.js.coffee is in place" do
    get :index
    assert_select '.store .entry > img', 3
    assert_select '.entry input[type=submit]', 3
  end

Run the tests again.

rails test
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)
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!':  (ActiveRecord::PendingMigrationError)

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
	from /home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
	from /home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
	from /home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `require'
	from /home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
	from /home/rubys/git/rails/railties/lib/rails/commands/test.rb:9:in `<top (required)>'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:138:in `require'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:138:in `require_command!'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:95:in `test'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
	from /home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
	from /home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'

Save our progress

git commit -a -m "AJAX"
[master 5ef642e] AJAX
 5 files changed, 57 insertions(+), 27 deletions(-)
 rewrite app/assets/javascripts/application.js (61%)
git tag iteration-f

12.1 Iteration G1: Capturing an Order 11.5 Iteration F5: Making Images Clickable