2017-12-06 17 views
1

私はRspecを初めて使用しています。私はTDDをやろうとしています。アプリケーションコントローラには、現在のユーザーを設定するという方法があります。NoMethodError:nilのための未定義メソッド `id ':NilClass

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    protected 
    def set_current_user 
     @current_user ||= User.find_by_session_token(cookies[:session_token]) 
     redirect_to login_path unless @current_user 
    end 
    end 

、ここBlogsController.rb

class BlogsController < ApplicationController 
    before_action :set_current_user 
    before_action :has_user_and_hobby 
    def blog_params 
    params.require(:blog).permit(:title, :hobby_id, :user_id, :body, :rating) 
    end 

... 

    def destroy 
    @blog = Blog.find(params[:id]) 
    if @blog.user_id != @current_user.id 
     flash[:notice] = "The blog #{@blog.title} only can be deleted by the author! It cannot be deleted by others." 
     redirect_to hobby_blogs_path(@blog) 
    else 
     @blog.destroy 
     flash[:notice] = "Blog '#{@blog.title}' deleted." 
     redirect_back(fallback_location: root_path) 
    end 
    end 
end 

と私は破壊のルートをテストするために書いたRSpecのは、次のとおりです。

require 'spec_helper' 
require 'rails_helper' 

describe BlogsController do 
    let(:fuser) { FactoryGirl.create(:fuser) } 
    let(:hobby) { FactoryGirl.create(:hobby)} 
    let(:blog) { FactoryGirl.create(:blog, hobby_id: hobby.id, user_id: fuser.id)} 
    let(:comment) { FactoryGirl.create(:comment)} 

... 

    describe 'delete a blog' do 
     before :each do 
      allow_any_instance_of(ApplicationController).to receive(:set_current_user).and_return(fuser) 
      allow_any_instance_of(BlogsController).to receive(:has_user_and_hobby).and_return(blog.user_id,hobby) 
      allow(User).to receive(:find).with(blog.user_id).and_return(blog.user_id) 

     it 'should redirect_back' do 
      delete :destroy, params:{:hobby_id =>hobby.id, :id => blog.id} 
      expect(response).to be_redirect 
     end 
    end 
end 

私はスペックを実行しようとすると、私が手エラー:

Failure/Error: if @blog.user_id != @current_user.id 
NoMethodError: 
    undefined method `id' for nil:NilClass 

anyoこれで私を助ける方法を知っていますか?すべての助けに大変感謝します。

答えて

3

@current_userはテストではnilです。

ここに問題があります。

allow_any_instance_of(ApplicationController).to receive(:set_current_user).and_return(fuser) 

set_current_userが実際にユーザーオブジェクトを返さない、それは、おそらく次にリダイレクトを@current_user変数に1を割り当て、。

それははるかにレールが邪魔このようにユーザーを設定して行います:あなたの現在のユーザーに署名した参照するとき

class ApplicationController < ActionController::Base 
    before_action :verify_current_user! 

    def current_user 
    @current_user || User.find_by_session_token(cookies[:session_token]) 
    end 

    def verify_current_user! 
    redirect_to login_path unless current_user 
    end 
end 

はその後、current_userメソッドを呼び出します。値はメモされるので、パフォーマンス上のペナルティはありません。また、テスト中にcurrent_userメソッドをスタブすることもできます。あなたのコントローラでは、@current_userの代わりに常にcurrent_userと呼んでください。

+0

これは機能します。どうもありがとうございます! – amazingPuppy

関連する問題