I cant get any of my session controller test to pass. one i created a a sessions helper module they started to fail. i keep getting the error
Failure/Error: post :create, email: email, password: password NoMethodError: undefined method `[]' for nil:NilClass
here is some of my code
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
def current_user
User.find_by(id: session[:user_id])
if @current_user.nil?
@current_user = @current_user || User.find_by(id: session[:user_id])
else
@current_user
end
end
def logged_in?
!current_user.nil?
end
end
This is the session controller test
require 'rails_helper'
require 'sessions_helper'
RSpec.describe SessionsController, type: :controller do
describe "GET #new" do
it "returns http success" do
get :new
expect(response).to have_http_status(:success)
end
it "renders the new template" do
get 'new'
expect(response).to render_template('new')
end
end
describe "POST 'create'" do
context "with correct credentials" do
let!(:user) {User.create(name: "daniel", email: "[email protected]", password: "rowland1", password_confirmation: "rowland1")}
it "redirects to the user path" do
post :create, email: "[email protected]", password: "rowland1"
log_in(user)
expect(response).to be_redirect
expect(response).to redirect_to(@user)
end
it "finds the user" do
User.expects(:find_by).with({email: "[email protected]"}).returns(user)
post :create, email: "[email protected]", password: "rowland1"
end
it "authenticates the user" do
User.stubs(:find_by).returns(user)
user.expects(:authenticate).once
post :create, email: "[email protected]", password: "rowland1"
end
it "sets the user_id in the session" do
post :create, email: "[email protected]", password: "rowland1"
expect(session[:user_id]).to eq(user.id)
end
end
it "sets the flash success message" do
post :create, email: "[email protected]", password: "rowland1"
expect(flash[:success]).to eq("Thanks for logging in!")
end
shared_examples_for "denied login" do
it "renders new template" do
post :create, email: email, password: password
expect(response).to render_template('new')
end
it "sets the flash danger message" do
post :create
expect(flash[:danger]).to eq("there was a problem logging in. Please check your email and password.")
end
end
context "with blank credentials" do
let(:email) {""}
let(:password) {""}
it_behaves_like "denied login"
end
context "with an incorrect password" do
let!(:user) {User.create(name: "daniel", email: "[email protected]", password: "rowland1", password_confirmation: "rowland1")}
let(:email) { user.email}
let(:password) {"incorrect"}
it_behaves_like "denied login"
end
context "with an incorrect email" do
let(:email) { "[email protected]"}
let(:password) {"incorrect"}
it_behaves_like "denied login"
end
end
end
The session controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email])
if user && user.authenticate(params[:session][:password])
log_in @user
flash[:success] = "Thanks for logging in!"
redirect_to user_path
else
flash.now[:danger] = "there was a problem logging in. Please check your email and password."
render 'new'
end
end
def destroy
end
end
the user controller
class UsersController < ApplicationController
def index
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def edit
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to @user, :flash => { :success => "Welcome to the Sample App!" }
else
@title = "Sign up"
render 'new'
end
end
def update
end
def destroy
end
private
# Use strong_parameters for attribute whitelisting
# Be sure to update your create() and update() controller methods.
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :avatar)
end
end
Rspec test failures
SessionsController POST 'create' with correct credentials redirects to the user path
Failure/Error: post :create, email: "[email protected]", password: "rowland1"
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:9:in `create'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack- 4.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:137:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionview-4.2.1/lib/action_view/rendering.rb:30:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:632:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/sessions_controller_spec.rb:25:in `block (4 levels) in <top (required)>'
3) SessionsController POST 'create' with correct credentials finds the user
Failure/Error: post :create, email: "[email protected]", password: "rowland1"
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:9:in `create'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:137:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionview-4.2.1/lib/action_view/rendering.rb:30:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:632:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/sessions_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
4) SessionsController POST 'create' with correct credentials authenticates the user
Failure/Error: post :create, email: "[email protected]", password: "rowland1"
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:9:in `create'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:137:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionview-4.2.1/lib/action_view/rendering.rb:30:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:632:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/sessions_controller_spec.rb:39:in `block (4 levels) in <top (required)>'
5) SessionsController POST 'create' with correct credentials sets the user_id in the session
Failure/Error: post :create, email: "[email protected]", password: "rowland1"
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:9:in `create'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'
# /Users/DanielPonce/.rvm/gems/ruby