Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

12.1 Iteration G1: Capturing an Order 11.4 Iteration F4: Hide an Empty Cart

11.5 Iteration F5: Testing AJAX changes

</24 tests, 40 assertions, 0 failures, 0 errors/> expected but was
<"  test \"should create line_item\" do">.

Traceback:
  /home/rubys/svn/rails4/Book/util/checkdepot.rb:135

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

get /products

NoMethodError in Products#index

Showing /home/rubys/svn/rails4/Book/util/work/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/svn/rails4/Book/util/work/depot

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:21
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 AJAX 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 'tr#current_item', /Programming Ruby 1.9/
  end

Run the tests again.

rake test
(in /home/rubys/svn/rails4/Book/util/work/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p249%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
........
Finished in 0.134033 seconds.
 
8 tests, 29 assertions, 0 failures, 0 errors
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p249%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
........E...............
Finished in 0.675055 seconds.
 
  1) Error:
test_should_create_line_item_via_ajax(LineItemsControllerTest):
ActionView::Template::Error: Missing partial carts/cart with {:handlers=>[:rjs, :rhtml, :rxml, :erb, :builder], :locale=>[:en, :en], :formats=>[:js]} in view paths "/home/rubys/svn/rails4/Book/util/work/depot/app/views"
    app/views/line_items/create.js.rjs:1
    app/views/line_items/create.js.rjs:1
    /test/functional/line_items_controller_test.rb:57:in `test_should_create_line_item_via_ajax'
    /test/functional/line_items_controller_test.rb:55:in `test_should_create_line_item_via_ajax'
 
24 tests, 37 assertions, 0 failures, 1 errors
Errors running test:functionals!

Save our progress

git commit -a -m "AJAX"
Created commit 13dcdf4: AJAX
 3 files changed, 39 insertions(+), 4 deletions(-)
git tag iteration-g

12.1 Iteration G1: Capturing an Order 11.4 Iteration F4: Hide an Empty Cart