Capybara :: ElementNotFound:无法找到字段" title"

时间:2014-12-01 11:22:38

标签: ruby-on-rails ruby rspec

我无法解决此问题。请帮我。它给了我找不到元素的错误。

规格/特征/待办事项/ create_spec.rb

require 'spec_helper'

describe "Creating todos" do

 let(:user) { FactoryGirl.create(:user)}
 before(:each) do
   visit root_path
   click_link "Login"

   fill_in "Email",  with:  "user@gmail.com"
   fill_in "Password",  with: "password"
   click_button "Sign in"
 end

  it "redirects to the todos index page" do
   visit "/todos"

   fill_in "title", with: "MyString"
   click_button "Create"

   expect(page).to have_title("MyString")
 end

我的观看代码。 _new_form.html.erb

     <%= form_for current_user.todos.new, remote:true  do |f| %>
       <div class="input-group">
         <%= f.text_field :title, class: "form-control",   placeholder: "title" %>
         <span class="input-group-btn">
         <%= f.submit "Create", class: "btn btn-success" %>
         </span>
       </div>
     <% end %>

spec_helper.rb

    def sign_in
      @user = FactoryGirl.create(:user)
      controller.stub(:authenticate_user!).and_return(true)
      @user
    end

3 个答案:

答案 0 :(得分:5)

如果您在本地服务器上运行代码,请检查该元素,该字段的名称是Capybara需要运行的内容。通常,如果表单是嵌套的,则会出现名称rails(在本例中)类似于todos[title]

因此,spec / features / todos / create_spec.rb应该类似于:

require 'spec_helper'
 ...

it "redirects to the todos index page" do
 visit "/todos"

 fill_in "todos[title]", with: "MyString"
 click_button "Create"

 expect(page).to have_title("MyString")
end

答案 1 :(得分:3)

找到我的答案。

在视图文件中,我有一个标题带有id的text_field。控制台中的id = todo_title 但我在测试中称呼称号。这里的水豚未能找到头衔。这是todo_title

使用后

fill_in "todo_title"

它就像魅力一样。

答案 2 :(得分:1)

在您的表单中,必须有一个名称为“title”的元素。来自docs

fill_in(locator, options = {}) ⇒ Object

Locate a text field or text area and fill it in with the given text The field can be found via its name, id or label text.