Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec accessing application_controller methods such as 'current_user'

I'm trying to stub out a method on my current_user (using a modified restful_authentication auth solution) with rspec. I'm completely unsure of how I can access this method in my controller specs. current_user by itself doesn't work. Do I need to get the controller itself first? How do I do this?

Using rails 2.3.5, rspec 1.3.0 and rspec-rails 1.3.2

# my_controller_spec.rb
require 'spec_helper'

describe MyController do

  let(:foos){ # some array of foos }

  it "fetches foos of current user" do
    current_user.should_receive(:foos).and_return(foos)
    get :show
  end
end

Produces

NoMethodError in 'ChallengesController fetches foos of current user'
undefined method `current_user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass_1::Subclass_2::Subclass_2:0x7194b2f4>
like image 327
brad Avatar asked Sep 15 '10 19:09

brad


2 Answers

rspec-rails gives you a controller method for use in controller examples. So:

controller.stub!(:current_user).with(:foos).and_return(foos)

ought to work.

like image 52
zetetic Avatar answered Nov 15 '22 03:11

zetetic


how can it know where to find current_user? this should solve it:

subject.current_user.should_receive(:foos).and_return(foos)
like image 23
oma Avatar answered Nov 15 '22 03:11

oma