如何让我的会话控制器测试使用Rspec传递

时间:2015-07-25 21:03:24

标签: ruby-on-rails ruby session login rspec

我无法通过任何会话控制器测试。一个我创建了一个会话助手模块,他们开始失败。我一直收到错误

失败/错误:发布:创建,电子邮件:电子邮件,密码:密码      NoMethodError:        未定义的方法`[]'为零:NilClass

这是我的一些代码

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

这是会话控制器测试

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:      "danielcoolness@yahoo.com", password: "rowland1", password_confirmation: "rowland1")}

    it "redirects to the user path" do
        post :create, email: "danielcoolness@yahoo.com", 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: "danielcoolness@yahoo.com"}).returns(user)
        post :create, email: "danielcoolness@yahoo.com", password: "rowland1"
    end 

    it "authenticates the user" do
        User.stubs(:find_by).returns(user)
        user.expects(:authenticate).once
        post :create, email: "danielcoolness@yahoo.com", password: "rowland1"
    end

    it "sets the user_id in the session" do
        post :create, email: "danielcoolness@yahoo.com", password: "rowland1"
        expect(session[:user_id]).to eq(user.id)
    end
  end  


    it "sets the flash success message" do
    post :create, email: "danielcoolness@yahoo.com", 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: "danielcoolness@yahoo.com", 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) { "no@found.com"}
        let(:password) {"incorrect"}
        it_behaves_like "denied login"
  end
end

会话控制器

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

用户控制器

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

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

Rspec测试失败

SessionsController POST 'create' with correct credentials redirects to the user path
 Failure/Error: post :create, email: "danielcoolness@yahoo.com", 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: "danielcoolness@yahoo.com", 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: "danielcoolness@yahoo.com", 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: "danielcoolness@yahoo.com", 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

1 个答案:

答案 0 :(得分:0)

您在哪里放置了SessionsHelper?如果它位于app/helpers内,那么view帮助器和此模块下的方法只能用于rails views,因此您的规格不会通过。

如果我错了,请提供您的rspec logs