Capybara::ElementNotFound: 无法找到字段 "user_email"
Capybara::ElementNotFound: Unable to find field "user_email"
我正在对我的注册页面进行测试,但出现此错误,提示找不到该字段。我在fill_in方法
中使用了输入id
<%= form_for @user, url: {action: "create"},html: {class: "horizontal-form", id: "signup-form"} do |f| %>
<div class="form-group">
<%= f.email_field :email, placeholder: "Email", class: "form-control" %>
<%= f.text_field :username, placeholder: "Username", class: "form-control" %>
<%= f.password_field :password, placeholder: "Password", class: "form-control" %>
<%= f.password_field :password_confirmation, placeholder: "Password Confirmation", class: "form-control" %>
<%= f.submit "Sign Up", class: "btn" %>
</div>
<% end %>
测试
require 'rails_helper'
RSpec.describe UsersController, type: :controller do
describe "GET Sign-Up" do
it "returns http success" do
visit '/signup'
get :new
expect(response).to have_http_status(:success)
end
end
describe "Post User" do
it "creates user" do
user_params = FactoryGirl.attributes_for(:user)
fill_in "user_email", with: "user_params[:email]"
fill_in "user_username", with: user_params[:username]
fill_in "user_password", with: user_params[:password_digest]
fill_in "user_password_confirmation", with: user_params[:password_digest]
click_button "Sign Up"
expect {
post :create, user: user_params
}.to change(User, :count).by(1)
expect(current_path).to redirect_to(root_path)
end
end
end
但我一直收到这个错误
1) UsersController GET Sign-Up returns http success
Failure/Error: fill_in "user_email", with: "user_params[:email]"
Capybara::ElementNotFound:
Unable to find field "user_email"
您在以下几行中所做的并不是真正的控制器规范。
fill_in "user_email", with: "user_params[:email]"
fill_in "user_username", with: user_params[:username]
fill_in "user_password", with: user_params[:password_digest]
fill_in "user_password_confirmation", with: user_params[:password_digest]
click_button "Sign Up"
它更像是一个功能规范,因为您使用的是模拟浏览器中用户行为的水豚。目前您正在混合功能和控制器规格,因此当您删除上面的那些行时,您的测试应该可以工作。
对于控制器规范,您将请求和参数直接发送到您正在测试的控制器,因为它们仅用于测试控制器本身,而不是与视图交互。
您可以在 rspec 文档中阅读更多关于差异的信息:
https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec
我猜您需要在表单可见之前单击页面上的 link 或按钮,但如果不查看更多页面就无法确认这一点 - 如果不是这种情况,请显示生成的 html 而不是 erb,因此我们可以看到字段的名称,因为它们出现在浏览器中。话虽这么说,你的测试不会像你写的那样正常工作,因为你混合了功能测试和控制器测试——你不能使用水豚方法来填充浏览器中的字段,也不能使用 get , post, 等在相同的测试中。使用水豚时,您需要执行用户会执行的操作,然后验证来自这些操作的屏幕更改。
我正在对我的注册页面进行测试,但出现此错误,提示找不到该字段。我在fill_in方法
中使用了输入id<%= form_for @user, url: {action: "create"},html: {class: "horizontal-form", id: "signup-form"} do |f| %>
<div class="form-group">
<%= f.email_field :email, placeholder: "Email", class: "form-control" %>
<%= f.text_field :username, placeholder: "Username", class: "form-control" %>
<%= f.password_field :password, placeholder: "Password", class: "form-control" %>
<%= f.password_field :password_confirmation, placeholder: "Password Confirmation", class: "form-control" %>
<%= f.submit "Sign Up", class: "btn" %>
</div>
<% end %>
测试
require 'rails_helper'
RSpec.describe UsersController, type: :controller do
describe "GET Sign-Up" do
it "returns http success" do
visit '/signup'
get :new
expect(response).to have_http_status(:success)
end
end
describe "Post User" do
it "creates user" do
user_params = FactoryGirl.attributes_for(:user)
fill_in "user_email", with: "user_params[:email]"
fill_in "user_username", with: user_params[:username]
fill_in "user_password", with: user_params[:password_digest]
fill_in "user_password_confirmation", with: user_params[:password_digest]
click_button "Sign Up"
expect {
post :create, user: user_params
}.to change(User, :count).by(1)
expect(current_path).to redirect_to(root_path)
end
end
end
但我一直收到这个错误
1) UsersController GET Sign-Up returns http success
Failure/Error: fill_in "user_email", with: "user_params[:email]"
Capybara::ElementNotFound:
Unable to find field "user_email"
您在以下几行中所做的并不是真正的控制器规范。
fill_in "user_email", with: "user_params[:email]"
fill_in "user_username", with: user_params[:username]
fill_in "user_password", with: user_params[:password_digest]
fill_in "user_password_confirmation", with: user_params[:password_digest]
click_button "Sign Up"
它更像是一个功能规范,因为您使用的是模拟浏览器中用户行为的水豚。目前您正在混合功能和控制器规格,因此当您删除上面的那些行时,您的测试应该可以工作。
对于控制器规范,您将请求和参数直接发送到您正在测试的控制器,因为它们仅用于测试控制器本身,而不是与视图交互。
您可以在 rspec 文档中阅读更多关于差异的信息:
https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec
我猜您需要在表单可见之前单击页面上的 link 或按钮,但如果不查看更多页面就无法确认这一点 - 如果不是这种情况,请显示生成的 html 而不是 erb,因此我们可以看到字段的名称,因为它们出现在浏览器中。话虽这么说,你的测试不会像你写的那样正常工作,因为你混合了功能测试和控制器测试——你不能使用水豚方法来填充浏览器中的字段,也不能使用 get , post, 等在相同的测试中。使用水豚时,您需要执行用户会执行的操作,然后验证来自这些操作的屏幕更改。