learn-co-curriculum / diy-json-serializer-lab

Other
0 stars 4 forks source link

First test in product_feature_spec.rb doesn't work #3

Open ivalentine opened 7 years ago

ivalentine commented 7 years ago

I was helping @jd2rogers2 with this lab, and we seem to have discovered an issue: the first test in product_feature_spec.rb doesn't work. Here's the test:

it 'requires javascript to go next' do
    p1 = Product.create!(name: "Test Product", inventory: 0, description: "This is a test description with more text than should be there.", price: "2.99")
    p2 = Product.create!(name: "Test Product 2", inventory: 1, description: "This is a second test description with more text than should be there.", price: "1.99")

    visit product_path(p1)
    expect(page).to have_content p1.name
    click_link "Next Product"
    expect(page).not_to have_content p2.name
  end

James and I both tested his code by opening the first product page in the browser, disabling JavaScript, and clicking the "Next Product" link. As expected, nothing happened, but the test is still failing.

This issue appears to be the same as learn-co-curriculum/using-active-model-serializer-lab#3.

mdo5004 commented 7 years ago

@pajamaw and I just ran into this issue, and discovered the reason was that my view was rendering

p1 = Product.create!(name: "Test Product", ... , price: "2.99")
p2 = Product.create!(name: "Test Product 2", ... )

with

<h1><%= @product.name %></h1>
<li><%= @product.price %></li>

as

page.text 
#=> Test Product 2 ... (the name and price of p1)

Which happens to be the name of p2, which the test is expecting the page not_to have.

Removing or simply moving the price in the view fixed this issue. e.g.

<h1><%= @product.name %></h1>
<li><%= @product.inventory %></li>
<li><%= @product.price %></li>

Suggested fix: change p2 to

p2 = Product.create!(name: "Test Product Two", ... )