Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Tom Finet
Tom Finet
7,027 Points

Test Fails. Why?

Ok, so I'm taking the rails course and I'm building the simple todo application. The tests to make sure that people enter proper information for todo items is supposed to be working but for some reason it's not.

Here is my code and my test results.

new.html.erb:

<%= form_for [@todo_list, @todo_item] do |form| %> <% if @todo_item.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being saved:</h2>

      <ul>
      <% @todo_item.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
<% end %>

<%= form.label :content %>
<%= form.text_field :content %>

<%= form.submit "Save" %>

<% end %>

create_spec.rb:

require 'spec_helper'

describe "Viewing todo items" do let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries") }

def visit_todo_list(list)
    visit "/todo_lists"
    within "#todo_list_#{list.id}" do
        click_link "List Items"
    end
end

it "is successful with valid content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "Milk"
    click_button "Save"
    expect(page).to have_content("Added todo list item")
    within("ul.todo_items") do
        expect(page).to have_content("Milk")
    end
end

it "displays an error with no content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: ""
    click_button "Save"
    within("div.flash") do
        expect(page).to have_content("There was a problem adding that todo list item.")
    end
    expect(page).to have_content("Content can't be blank")          
end

end

todo_item.rb:

class TodoItem < ActiveRecord::Base belongs_to :todo_list validates :content, presence: true end

test results:

Toms-MacBook-Air:Unico tomfinet$ bin/rspec --format=documentation spec/features/todo_items/create_spec.rb

Viewing todo items is successful with valid content displays an error with no content (FAILED - 1)

Failures:

1) Viewing todo items displays an error with no content Failure/Error: expect(page).to have_content("There was a problem adding that todo list item.") expected to find text "There was a problem adding that todo list item." in "" # ./spec/features/todo_items/create_spec.rb:30:in block (3 levels) in <top (required)>' # ./spec/features/todo_items/create_spec.rb:29:inblock (2 levels) in <top (required)>'

Deprecation Warnings:


RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3. There are a few options for what you can use instead:

  • rspec-core's DSL methods (it, before, after, let, subject, etc) now yield the example as a block argument, and that is the recommended way to access the current example from those contexts.
  • The current example is now exposed via RSpec.current_example, which is accessible from any context.
  • If you can't update the code at this call site (e.g. because it is in an extension gem), you can use this snippet to continue making this method available in RSpec 2.99 and RSpec 3:

    RSpec.configure do |c| c.expose_current_running_example_as :example end

(Called from /Users/tomfinet/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/capybara-2.1.0/lib/capybara/rspec.rb:20:in `block (2 levels) in <top (required)>')


RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3. There are a few options for what you can use instead:

  • rspec-core's DSL methods (it, before, after, let, subject, etc) now yield the example as a block argument, and that is the recommended way to access the current example from those contexts.
  • The current example is now exposed via RSpec.current_example, which is accessible from any context.
  • If you can't update the code at this call site (e.g. because it is in an extension gem), you can use this snippet to continue making this method available in RSpec 2.99 and RSpec 3:

    RSpec.configure do |c| c.expose_current_running_example_as :example end

(Called from /Users/tomfinet/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/capybara-2.1.0/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>')

If you need more of the backtrace for any of these deprecations to identify where to make the necessary changes, you can configure config.raise_errors_for_deprecations!, and it will turn the deprecation warnings into errors, giving you the full backtrace.

2 deprecation warnings total

Finished in 0.3458 seconds 2 examples, 1 failure

Failed examples:

rspec ./spec/features/todo_items/create_spec.rb:24 # Viewing todo items displays an error with no content

Randomized with seed 34131

1 Answer

Seth Reece
Seth Reece
32,867 Points

Hi Sarah,

It looks like in your test:

within("div.flash") do

is looking for div with the class "flash".

It doesn't look like you have one. Try:

<div id="error_explanation" class="flash">

on line 2 of new.html.erb.

Tom Finet
Tom Finet
7,027 Points

Your suggestion doesn't seem to be working. Instead it gives me this error:

Toms-MacBook-Air:Unico tomfinet$ bin/rspec --format=documentation spec/features/todo_items/create_spec.rb

Viewing todo items is successful with valid content displays an error with no content (FAILED - 1)

Failures:

1) Viewing todo items displays an error with no content Failure/Error: within("div.flash") do Capybara::Ambiguous: Ambiguous match, found 2 elements matching css "div.flash" # ./spec/features/todo_items/create_spec.rb:29:in `block (2 levels) in <top (required)>'

Seth Reece
Seth Reece
32,867 Points

Capybara is saying that it found two divs with the class of flash.

Tom Finet
Tom Finet
7,027 Points

So what is the solution.