view.stub in rails partial rspec gives 'undefined method view_context'

浪尽此生 提交于 2020-01-03 17:12:59

问题


using Rails 3.2.11

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

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.

I get:

Failure/Error: view.stub(:current_user) { @client }
 NoMethodError:
   undefined method `view_context' for nil:NilClass

Here is the line from the view where current_user is used for completeness:

<% if my_project.user_id == current_user.id %> 

Does anyone know how I can get it to stub current_user successfully, I'm at a loss here...

thanks


回答1:


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.



来源:https://stackoverflow.com/questions/15066417/view-stub-in-rails-partial-rspec-gives-undefined-method-view-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!