Search code examples
ruby-on-railsrubyruby-on-rails-3rspecrspec-rails

Rspec testing fail strong params


Trying to get this one to pass for a while, not that familiar with Rspec, tried couple answers, and no luck yet, I have these two parameters being required on my controller as follow:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_back fallback_location: new_user_path
    else
      render :new
    end
  end
private
  def user_params
    params.require(:user).permit(:username, :password)
  end
end

and this is the rspec test that is giving me an issue:

  describe "GET #create" do
    it "returns http success" do
      get :create
      expect(response).to have_http_status(:success)
    end
  end

here is the error that I am getting:

1) UsersController GET #create returns http success
     Failure/Error: params.require(:user).permit(:username, :password)

     ActionController::ParameterMissing:
       param is missing or the value is empty: user

I am looking at the rspec documentation right now, but any help would be appreciated it.


Solution

  • Try this:

    describe "POST #create" do
      it "returns http success" do
        post :create, params: { user: { username: "name", password: "password" } }
        expect(response).to have_http_status(:success)
      end
    end
    

    It should be post :create, not get :create