I'm coding a Rails 4 application to learn Rails & testing. My program code works as expected, but I can't figure out why I'm getting a no method error when posting to the create method in a Sessions controller test (RSpec v. 3.1.0) Here's the text of the error:
Failure/Error: post :create, email: "testerx@tester-x.net", password: "passwordx"
NoMethodError:
undefined method `[]' for nil:NilClass
This is relevant code from my Sessions Controller spec:
describe "POST create" do
context "with correct credentials" do
let!(:user) { User.create(user_name: "Testerx", email: "testerx@tester-x.net", password: "passwordx", password_confirmation: "passwordx", workout_enthusiast: "true" ) }
it "redirects to user show page" do
post :create, email: "testerx@tester-x.net", password: "passwordx"
expect(response).to be_redirect
expect(response).to redirect_to(users_show_path)
end
This is my Sessions Controller code:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Logs the user in and redirects to the user's show page.
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
The error says undefined method for nil:NilClass. I'm sure the user is valid. I can't figure out why posting to the create method is not working in the test scenario. It works as expected in the application. What am I missing?
Aucun commentaire:
Enregistrer un commentaire