【问题标题】:Capybara::ElementNotFound: Unable to find field "user_email"Capybara::ElementNotFound:找不到字段“user_email”
【发布时间】:2015-10-06 21:32:52
【问题描述】:

我正在对我的注册页面进行测试,但我收到了这个错误,提示它找不到该字段。我在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"

【问题讨论】:

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


    【解决方案1】:

    您在以下几行中所做的并不是真正的控制器规范。

     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"
    

    这更像是一个功能规范,因为您使用的是在浏览器中模拟用户行为的 capybara。目前您正在混合功能和控制器规格,因此当您删除测试上方的这些行时应该可以工作。

    对于控制器规范,您将请求和参数直接发送到您正在测试的控制器,因为它们仅用于测试控制器本身,而不是与视图交互。

    您可以在 rspec 文档中阅读更多关于差异的信息:

    https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec

    【讨论】:

    • 我添加了visit '/signup',但仍然是同样的错误
    • 你能去掉 fill_in 和 click_button 行吗?测试不需要它们。
    【解决方案2】:

    我猜您需要在表单可见之前单击页面上的链接或按钮,但如果没有看到更多页面则无法确认 - 如果不是这种情况,则显示生成的 html 而不是erb 这样我们就可以在浏览器中看到字段的名称。话虽如此,您的测试不会像您编写它们的方式那样正常工作,因为您混合了功能测试和控制器测试——您不能使用 capybara 方法来填写浏览器中的字段,也不能使用 get , post 等在相同的测试中。使用 capybara 时,您需要执行用户会执行的操作,然后验证来自这些操作的屏幕变化。

    【讨论】:

    • 感谢您提供的信息,我刚开始测试,所以我还不太擅长...再次感谢
    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多