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

NoMethodError in Products#index

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

  undefined method `line_items' for nil:NilClass

Extracted source (around line #21):

  18:     <div id="side">
19: <!-- START_HIGHLIGHT -->
20:       <!-- START:hidden_div -->
21:       <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
22:         <%= render @cart %>
23:       <% end %>
24:     <!-- END:hidden_div -->

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

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___1160470672154448099_70330161124220'
app/controllers/products_controller.rb:7:in `index'

Request

Parameters:

None

Show session dump

Show env dump

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/functional/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/functional/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/functional/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.

rake test
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
 
CartTest:
     PASS add duplicate product (0.27s) 
     PASS add unique products (0.01s) 
 
ProductTest:
     PASS image url (0.04s) 
     PASS product attributes must not be empty (0.00s) 
     PASS product is not valid without a unique title (0.00s) 
     PASS product is not valid without a unique title - i18n (0.00s) 
     PASS product price must be positive (0.00s) 
 
Finished in 0.328884 seconds.
 
7 tests, 28 assertions, 0 failures, 0 errors, 0 skips
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
 
CartsControllerTest:
     PASS should create cart (0.23s) 
     PASS should destroy cart (0.05s) 
     PASS should get edit (0.09s) 
     PASS should get index (0.01s) 
     PASS should get new (0.01s) 
     PASS should show cart (0.01s) 
     PASS should update cart (0.01s) 
 
LineItemsControllerTest:
     PASS should create line item (0.01s) 
     PASS should create line item via ajax (0.09s) 
     PASS should destroy line item (0.00s) 
     PASS should get edit (0.01s) 
     PASS should get index (0.01s) 
     PASS should get new (0.01s) 
     PASS should show line item (0.01s) 
     PASS should update line item (0.01s) 
 
ProductsControllerTest:
     PASS can't delete product in cart (0.01s) 
     PASS should create product (0.04s) 
     PASS should destroy product (0.00s) 
     PASS should get edit (0.01s) 
     PASS should get index (0.01s) 
     PASS should get new (0.01s) 
     PASS should show product (0.01s) 
     PASS should update product (0.01s) 
 
StoreControllerTest:
     PASS markup needed for store.js.coffee is in place (0.02s) 
     PASS should get index (0.01s) 
 
Finished in 0.671230 seconds.
 
25 tests, 42 assertions, 0 failures, 0 errors, 0 skips

Save our progress

git commit -a -m "AJAX"
[master ce3f07a] AJAX
 5 files changed, 48 insertions(+), 20 deletions(-)
 rewrite app/assets/javascripts/application.js (67%)
git tag iteration-f

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