简体   繁体   English

如何使我的会话控制器测试通过Rspec通过

[英]how to get my sessions controller test to pass using Rspec

I cant get any of my session controller test to pass. 我无法通过任何会话控制器测试。 one i created aa 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 失败/错误:发布:创建,电子邮件:电子邮件,密码:密码NoMethodError:未定义的方法'[]'为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:      "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

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

Where have you placed your SessionsHelper ? 您将SessionsHelper放在哪里? If it's inside app/helpers it's a view helper and methods under this module will only be available to rails views and hence your specs will not pass. 如果位于app/helpers内部,则它是view帮助器,并且此模块下的方法仅可用于rails views ,因此您的规格不会通过。

If I am wrong would you please provide your rspec logs . 如果我错了,请提供您的rspec logs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM