Stuck - bin / rake不会通过Authenticating Rails ODOT

时间:2014-08-15 15:22:19

标签: ruby-on-rails ruby ruby-on-rails-3 rspec capybara

对不起下面的代码呕吐,但我真的需要第二眼看到这个问题。感谢您的时间和帮助!我知道这是一个虚构的树屋应用程序的一个小问题,但是当我运行bin / rake时它让我感到窒息。如果有人有任何想法,请告诉我。谢谢!

 1) Adding todo items is successful with valid content
 Failure/Error: within("ul.todo_items") do
 Capybara::ElementNotFound:
   Unable to find css "ul.todo_items"
 # ./spec/features/todo_items/create_spec.rb:14:in `block (2 levels) in <top (required)>'

  2) todo_lists/index renders a list of todo_lists
 Failure/Error: assert_select "tr>td", :text => "Title".to_s, :count => 2
 Minitest::Assertion:
    Expected exactly 2 elements matching "tr > td", found 0..
    Expected: 2
   Actual: 0
  # ./spec/views/todo_lists/index.html.erb_spec.rb:20:in `block (2 levels) in <top (required)>'

  3) Viewing todo items displays item content when a todo list has items
  Failure/Error: expect(page.all("ul.todo_items li").size).to eq(2)

   expected: 2
        got: 0

   (compared using ==)
 # ./spec/features/todo_items/index_spec.rb:25:in `block (2 levels) in <top (required)>'

  4) Viewing todo items displays the title of the todo list
  Failure/Error: within("h1") do
  Capybara::Ambiguous:
    Ambiguous match, found 2 elements matching css "h1"
   # ./spec/features/todo_items/index_spec.rb:9:in `block (2 levels) in <top (required)>'

这是我的:app / views / todo_lists / index.html.erb

<h1>Todo Lists</h1>


<% @todo_lists.each do |todo_list| %>
<div class="todo_list" id="<%= dom_id(todo_list) %>">
<h2><%= todo_list.title %></h2>
<p><%= todo_list.description %></p>
 <ul class="functions">
    <li><%= link_to "List Items", todo_list_todo_items_path(todo_list) %></li>
    <li><%= link_to 'Show', todo_list %></li>
    <li><%= link_to 'Edit', edit_todo_list_path(todo_list) %></li>
    <li><%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %></li>
</ul>
    <br class="clear" />

 </div>
 <% end %>

 <br>

  <%= link_to 'New Todo list', new_todo_list_path %>

这是我的:spec / features / todo_items / index_spec.rb

require 'spec_helper'

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


it "displays the title of the todo list" do
        visit_todo_list(todo_list)
    within("h1") do
        expect(page).to have_content(todo_list.title)
    end
end

 it "displays no items when a todo list is empty" do    
     visit_todo_list(todo_list)
     expect(page.all("ul.todo_items li").size).to eq(0)
 end

 it "displays item content when a todo list has items" do
    todo_list.todo_items.create(content: "Milk")
    todo_list.todo_items.create(content: "Eggs")

     visit_todo_list(todo_list)

     expect(page.all("ul.todo_items li").size).to eq(2)

     within "ul.todo_items" do
         expect(page).to have_content("Milk")
         expect(page).to have_content("Eggs")
     end
    end
  end

这是我的:/ spec / features / todo_items / create_spec

require 'spec_helper'

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



 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

it "displays an error with content less than 2 characters long" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "1"
    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 is too short")
 end
 end

这是我的规范/观点/ todo_lists / index.html.erb_spec.rb

require 'spec_helper'

describe "todo_lists/index" do
  before(:each) do
  assign(:todo_lists, [
      stub_model(TodoList,
    :title => "Title",
    :description => "MyText"
  ),
  stub_model(TodoList,
    :title => "Title",
    :description => "MyText"
  )
])
 end

  it "renders a list of todo_lists" do
    render
    # Run the generator again with the --webrat flag if you want to use webrat matchers
     assert_select "tr>td", :text => "Title".to_s, :count => 2
     assert_select "tr>td", :text => "MyText".to_s, :count => 2
  end
end

1 个答案:

答案 0 :(得分:0)

有不同的问题,我将逐步完成它们

1) Adding todo items is successful with valid content
 Failure/Error: within("ul.todo_items") do
 Capybara::ElementNotFound:
   Unable to find css "ul.todo_items"
 # ./spec/features/todo_items/create_spec.rb:14:in `block (2 levels) in <top (required)>'

显然,CSS类在视图中发生了变化。您应该在规范中反映出来并将todo_items更改为functions


  2) todo_lists/index renders a list of todo_lists
 Failure/Error: assert_select "tr>td", :text => "Title".to_s, :count => 2
 Minitest::Assertion:
    Expected exactly 2 elements matching "tr > td", found 0..
    Expected: 2
   Actual: 0
  # ./spec/views/todo_lists/index.html.erb_spec.rb:20:in `block (2 levels) in <top (required)>'

todo项目可能在前一段时间在一张桌子上,现在它只是一个<div class="todo_list">。您应该在规范中匹配:

  it "renders a list of todo_lists" do
    render
    # Run the generator again with the --webrat flag if you want to use webrat matchers
     assert_select "div.todo_list", :text => "Title".to_s, :count => 2
     assert_select "div.todo_list", :text => "MyText".to_s, :count => 2
  end

  3) Viewing todo items displays item content when a todo list has items
  Failure/Error: expect(page.all("ul.todo_items li").size).to eq(2)

   expected: 2
        got: 0

   (compared using ==)
 # ./spec/features/todo_items/index_spec.rb:25:in `block (2 levels) in <top (required)>'

同样的问题,与div.todo_list匹配,因为ul.todo_items不存在:

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("div.todo_list") do
         expect(page).to have_content("Milk")
     end
 end

4) Viewing todo items displays the title of the todo list
  Failure/Error: within("h1") do
  Capybara::Ambiguous:
    Ambiguous match, found 2 elements matching css "h1"
   # ./spec/features/todo_items/index_spec.rb:9:in `block (2 levels) in <top (required)>'

此规范中的h1应为h2

it "displays the title of the todo list" do
        visit_todo_list(todo_list)
    within("h2") do
        expect(page).to have_content(todo_list.title)
    end
end