[rspec-users] Controller spec with devise.

David Chelimsky dchelimsky at gmail.com
Fri Aug 27 09:11:04 EDT 2010


On Aug 27, 2010, at 5:35 AM, Titinux wrote:

> I tink it's @orders should be an array of orders from
> current_user.orders.
> 
> describe OrdersController do
>  include Devise::TestHelpers
> 
>  def mock_order(stubs={})
>    @mock_order ||= mock_model(Order, stubs).as_null_object
>  end
> 
>  describe "GET index" do
>    it "assigns user's orders to @orders" do
>      current_user.stub!(:orders).and_return([mock_order])
> 
>      get :index
>      assigns(:orders).should eq([mock_order])
>    end
>  end
> end
> 
> I tried with this code but the test failed with this error :
> 
> Failure/Error: current_user.stub!(:orders).and_return([mock_order])
> undefined local variable or method `current_user' for
> #<RSpec::Core::ExampleGroup::Nested_7::Nested_1:0x000000057059e0>

Right. That's because there is no current_user in the example, it's in the controller. Make sense? So you need to stub current_user in the controller as well:

  user = double('user')
  controller.stub(:current_user) { user }
  user.stub(:orders) { [mock_order] }

  get :index

  assigns(:orders).should eq([mock_order])

HTH,
David

> On 26 août, 15:28, David Chelimsky <dchelim... at gmail.com> wrote:
>> On Aug 24, 2010, at 6:51 PM, Titinux wrote:
>> 
>> 
>> 
>>> Hello,
>> 
>>> I'm new in using RSpec and I can't figured out to spec this controller
>>> action.
>> 
>>> class OrdersController < ApplicationController
>>>  before_filter :authenticate_user!
>> 
>>>  def index
>>>    respond_with(@orders = current_user.orders)
>>>  end
>>> end
>> 
>>> When I want to spec this "@assets = Asset.all" I use "Asset.stub(:all)
>>> { [mock_asset] }" as I read in the RSpec book but with
>>> "current_user.orders" I don't know how to do.
>> 
>>> NB: I'm using Rails 3.0.0.rc2 and RSpec 2.0.0.beta.20
>> 
>> What is the behaviour you want to specify?
>> _______________________________________________
>> rspec-users mailing list
>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list