0
votes

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"
2

2 Answers

1
votes

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

1
votes

I'm guessing you need to click a link or button on the page before the form becomes visible, but can't confirm that without seeing more of the page - If thats not the case then show the generated html rather than the erb so we can see the names of the fields as they appear in the browser. That being said, your tests aren't going to work properly the way you're writing them since you're mixing up feature tests and controller tests -- you can't use capybara methods to fill in fields in browsers and also use get, post, etc. in the same tests. When using capybara you need to be doing the actions that users would do and then verifying the on screen changes that come from those actions.