gpt4 book ai didi

ruby-on-rails - capybara ::ElementNotFound:无法找到字段 "title"

转载 作者:数据小太阳 更新时间:2023-10-29 08:17:15 26 4
gpt4 key购买 nike

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

spec/features/todos/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

最佳答案

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

关于ruby-on-rails - capybara ::ElementNotFound:无法找到字段 "title",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27227328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com