4

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

bobomoreno
  • 2,848
  • 5
  • 23
  • 42
  • Is `@user` a test double/mock model? – Paul Fioravanti Feb 25 '13 at 12:05
  • hi, It's a FactoryGirl created model. – bobomoreno Feb 25 '13 at 12:13
  • Can you post any other relevant code, like your `User` factory, or how you're accessing `current_user` (by a method call to `current_user` or with an instance variable `@current_user`...), and perhaps a snippet of the view code you're attempting to stub. Also, do none of the SO answers [here](http://stackoverflow.com/q/12329515/567863) or [here](http://stackoverflow.com/q/10537932/567863), or [this blog post](http://blog.joeygeiger.com/2012/03/30/rspec-view-spec-and-dealing-with-current_user/) assist you? – Paul Fioravanti Feb 25 '13 at 12:31
  • hi, added full test code and some more info; the referenced solutions do not work for me unfortunately. – bobomoreno Feb 25 '13 at 13:57
  • Anything change if you change your view code to remove the call to `id`? ie `<% if my_project.user_id == current_user %>` – Paul Fioravanti Feb 25 '13 at 22:41
  • Hi, thanks, no, it doesn't make a difference. The error occurs in the before block, on view.stub, even before it gets to any testing. – bobomoreno Feb 27 '13 at 10:11
  • Hmm...how about just performing an assignment to `current_user` instead of stubbing it out: `view.current_user = @client`? – Paul Fioravanti Feb 28 '13 at 00:17
  • Failure/Error: view.current_user = @client NoMethodError: undefined method `view_context' for nil:NilClass. Same again view is NULL, I'm starting to think you cannot use 'view' with partials...? – bobomoreno Feb 28 '13 at 10:09
  • Looking at the [RSpec docs for specs with view that accesses helper_method helpers](https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/view-specs/view-spec#spec-with-view-that-accesses-helper-method-helpers), I'm wondering whether the `stub(:current_user)` needs to happen on either a `controller` or `helper` object (depending on where the method is. – Paul Fioravanti Feb 28 '13 at 10:22

1 Answers1

6

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.

bobomoreno
  • 2,848
  • 5
  • 23
  • 42