rails 部分 rspec 中的 view.stub 给出了“未定义的方法 view_context" [英] view.stub in rails partial rspec gives 'undefined method view_context'
问题描述
使用 Rails 3.2.11
using Rails 3.2.11
我有几个视图 rspec 测试,我需要在其中存根 'current_user' 调用.
I have a couple of view rspec tests where I need to stub the 'current_user' call.
我在常规视图测试中成功地使用了它,如下所示:
I've used this successfully in a regular view test like so:
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 是由 Devise 在 controllers/helpers.rb(在 gem 中)定义的.我在视图或控制器中到处使用它作为 current_user(作为方法,而不是实例).
Current_user is defined by Devise in controllers/helpers.rb (in the gem). I use it all over the place as current_user (as a method, not instance) in the view or controller.
问题似乎是在 view.stub 中的视图在这里为零,是否还有另一个对象用于局部?我只是不明白为什么这在常规视图中是完美的,而不是在局部视图中.
The problem seems to be around view in view.stub being nil here, is there another object that is used in case of partials?? I simply don't understand why this works perfect in a regular view and not in a partial.
我明白了:
Failure/Error: view.stub(:current_user) { @client }
NoMethodError:
undefined method `view_context' for nil:NilClass
这是视图中使用 current_user 来保证完整性的行:
Here is the line from the view where current_user is used for completeness:
<% if my_project.user_id == current_user.id %>
有谁知道我如何让它成功存根 current_user,我在这里不知所措...
Does anyone know how I can get it to stub current_user successfully, I'm at a loss here...
谢谢
推荐答案
原来移动 view.stub(:current_user) { @client } 到每个 'it' 块解决了问题;如果它在 'before:each/all' 块中,它似乎不起作用.
Turns out that moving view.stub(:current_user) { @client } into each 'it' block solved the problem; it does not seem to work if it is in the 'before:each/all' block.
这篇关于rails 部分 rspec 中的 view.stub 给出了“未定义的方法 view_context"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!