2015-10-06 124 views
0

我正在做我的注册页面的测试,但我得到这个错误,说它找不到该字段。我使用的输入ID在FILL_IN方法水豚:: ElementNotFound:无法找到字段“user_email”

<%= 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" 

回答

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" 

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

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

你可以阅读更多关于RSpec的文档中的差异:我猜你需要点击页面上的链接或按钮之前的形式变得可见

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

+0

我添加了'visit'/ signup'',但仍然是相同的错误 – eustass

+0

你可以删除fill_in和click_button行吗?他们不需要进行测试。 –

1

,但可如果没有看到更多的页面,确认没有 - 如果不是这种情况,那么显示生成的html而不是erb,这样我们就可以看到它们在浏览器中出现的字段名称。这就是说,你的测试不会按照你编写它们的方式正常工作,因为你正在混合功能测试和控制器测试 - 你不能使用capybara方法来填充浏览器中的字段,也可以使用get ,帖子等在相同的测试中。在使用水豚时,你需要做的是用户要做的动作,然后验证来自这些动作的屏幕变化。

+0

谢谢你提供的信息,我刚开始测试,所以我还没有那么好......再次感谢 – eustass

相关问题