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

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

get /products

HTTP Response Code: 500

NoMethodError in Products#index

Showing /home/rubys/git/awdwr/edition4/work-224-42/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-224-42/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
  test "should create line_item" do
    assert_difference('LineItem.count') do
      post :create, product_id: products(:ruby).id
    end
 
    assert_redirected_to store_path
  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.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.

rake test
Run options: --seed 8937
 
# Running:
 
................................
 
Finished in 0.479093s, 66.7929 runs/s, 166.9823 assertions/s.
 
32 runs, 80 assertions, 0 failures, 0 errors, 0 skips

Save our progress

git commit -a -m "AJAX"
[master 5ffba66] AJAX
 5 files changed, 48 insertions(+), 19 deletions(-)
git tag iteration-f

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