Rails3.2.11を使用する
'current_user'呼び出しをスタブする必要があるビューrspecテストがいくつかあります。
私はこれを次のような通常のビューテストで正常に使用しました。
require 'spec_helper'
describe "projects/_my_project.html.erb" do
before(:each) do
@client = FactoryGirl.create(:user)
view.stub(:current_user) { @client }
end
describe "new proposals notice" do
it "does not display new_propsals if in state posted and clicked after last submitted" do
@my_project = FactoryGirl.build(:project, status: "posted", last_proposals_click: "2012-02-02 14:01:00", last_proposal_submitted: "2012-02-02 14:00:00")
render :partial => "/projects/my_project", :locals => { :my_project => @my_project }
rendered.should_not have_content "You received new proposals"
end
end
end
Current_userは、(gemの)controllers/helpers.rbのDeviseによって定義されます。ビューまたはコントローラーのcurrent_userとして(インスタンスではなくメソッドとして)、あらゆる場所で使用しています。
問題はview.stubのビューの周りにあるようですが、ここではnilですが、パーシャルの場合に使用される別のオブジェクトはありますか?部分的にではなく、通常のビューでこれが完全に機能する理由がわかりません。
私は得る:
Failure/Error: view.stub(:current_user) { @client }
NoMethodError:
undefined method `view_context' for nil:NilClass
完全を期すためにcurrent_userが使用されているビューの行は次のとおりです。
<% if my_project.user_id == current_user.id %>
current_userを正常にスタブする方法を誰かが知っていますか、私はここで途方に暮れています...
ありがとう