Capybara :: ElementNotFound:无法找到字段“title”

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

规格/function/待办事项/ 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

   

spec_helper.rb

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

如果您在本地服务器上运行代码,请检查元素,该字段的名称是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 

找到我的答案。

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

使用后

 fill_in "todo_title" 

它像魅力一样工作。

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

 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.