small medium large xlarge

Generic-user-small
03 Sep 2017, 01:08
My Full Name (6 posts)

Failure: StoreControllerTest#test_should_get_index [/Users/gscar/Documents/Ruby/Ruby-Copeland/work/depot/test/controllers/store_controller_test.rb:9]: Expected at least 4 elements matching "nav.side_nav a", found 0.. Expected 0 to be >= 4.

The page displays correctly store_controller_test.rb

require 'test_helper'

class StoreControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get store_index_url
    assert_response :success
    assert_select 'h2', 'Programming Ruby 1.9'
    assert_select '.price', /\$[,\d]+\.\d\d/
    assert_select 'nav.side_nav a', minimum: 4 # moved down since failing. Above this passes
    assert_select 'main ul.catalog li', 3
  end

end

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Pragprog Books Online Store</title>
    <%= csrf_meta_tags %>

    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all',
    'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body class="<%= controller.controller_name %>">
    <div id="banner">
      <%= image_tag 'logo.svg', alt: 'The Pragmatic Bookshelf' %>
      <span class="title"><%= @page_title %></span>
    </div>
    <div id="columns">
      <div id="side">
        <ul>
          <li><a href="http://www....">Home</a></li>
          <li><a href="http://www..../faq">Questions</a></li>
          <li><a href="http://www..../news">News</a></li>
          <li><a href="http://www..../contact">Contact</a></li>
        </ul>
      </div>
      <div id="main">
        <%= yield %>
      </div>
    </div>
  </body>
</html>

store_controller_test.rb

require 'test_helper'

class StoreControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get store_index_url
    assert_response :success
    assert_select 'h2', 'Programming Ruby 1.9'
    assert_select '.price', /\$[,\d]+\.\d\d/
    assert_select 'nav.side_nav a', minimum: 4 # moved down since failing. Above this passes
    assert_select 'main ul.catalog li', 3
  end

end

I changed the order. Only the last two assertions fail.

store.index.erb

<% if notice %>
  <aside id="notice"><%= notice %></aside>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<ul class="catalog">
  <% @products.each do |product| %>
<li>
  <%= image_tag(product.image_url) %>
  <h2><%= product.title %></h2>
  <p>
    <%= sanitize(product.description) %>
  </p>
  <div class="price">
    <%= number_to_currency(product.price) %>
  </div> 
</li>
  <% end %>
</ul>
Generic-user-small
03 Sep 2017, 01:11
My Full Name (6 posts)

PS: You can also shows lines of code by indenting four spaces. I usually indent the original before copying, then undo the change.

Generic-user-small
03 Sep 2017, 17:38
My Full Name (6 posts)

Partial answer. I suspect some of the application.html.erb CSS ids and classes have changed names. I don’t completely get how to specify those for the test, but by trial and error I found that these changes resulted in the tests passing. Can’t figure how to do nested ids

require 'test_helper'

class StoreControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get store_index_url
    assert_response :success
    # assert_select 'nav.side_nav a', minimum: 4 # fails
    assert_select '#side a', minimum: 4 # passes. Can't figure out how to do nested ids
    # assert_select '#columns#side a', minimum: 4 # Fails. Can't figure out how to do nested ids
    # assert_select 'main ul.catalog li', 3 # fails
    assert_select '#main ul.catalog li', 3 # passes
    assert_select 'h2', 'Programming Ruby 1.9'
    assert_select '.price', /\$[,\d]+\.\d\d/
  end
end
Dmfcb_pragsmall
03 Sep 2017, 18:21
David Copeland (433 posts)

I just tried to run the tests in the depot_e folder in the downloadable code. I believe this is where you are in the book. All the tests pass for me. Can you see if there are some differences in your versions of the files? .side_nav is in app/layouts/application.html.erb, and it was added during iteration C2.

Generic-user-small
04 Sep 2017, 03:52
My Full Name (6 posts)

@David: so it is in C2. I copied from the downloaded code (which is what I posted above) and didn’t pay enough attention to what was written in the pdf.

Turns out the code I downloaded is older (5/19/17). I bought the book less than two weeks ago and downloaded the code at the same time—guess the code was late to the site. Anyway I now have the current code (8/21/17) and an answer to my problem. But it made me look harder at the code, so I learned something.

Thank you for answering. Enjoying the book.

Dmfcb_pragsmall
05 Sep 2017, 13:21
David Copeland (433 posts)

Oh, interesting. Yeah, I thought the book and downloadable code were in sync, but I guess there’s a delay? Sorry about that!

You must be logged in to comment