簡體   English   中英

如何使我的會話控制器測試通過Rspec通過

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

我無法通過任何會話控制器測試。 我創建了一個會話幫助程序模塊,但它們開始失敗了。 我不斷收到錯誤

失敗/錯誤:發布:創建,電子郵件:電子郵件,密碼:密碼NoMethodError:未定義的方法'[]'為nil: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

您將SessionsHelper放在哪里? 如果位於app/helpers內部,則它是view幫助器,並且此模塊下的方法僅可用於rails views ,因此您的規格不會通過。

如果我錯了,請提供您的rspec logs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM