I'm making a test for my signup page but I get this error that says it can't find the field. I used the input id in the fill_in method
<%= 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 %>
The test
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
But i keep getting this error
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"
What you're doing in the following lines is not really a controller spec.
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"
It's more of a feature spec, cause you're using capybara which simulates user behaviour in the browser. Currently you're mixing feature and controller specs, so when you remove those lines above your test should work.
For controller specs you send requests and params directly to the controller you're testing, since they're only meant to test the controller itself instead of interacting with the view.
You can read more about the difference in the rspec documentation:
https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec