Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec controller test with devise authentication

I am having problem with rspec testing controller the devise authentication.

I have a following setup

I have included

config.include Devise::TestHelpers, :type => :controller

in my spec_helper.rb

In my merchants_controller_spec.rb

describe MerchantsController do
  before :each do
    @user = Factory(:user)
    @merchant = Factory(:merchant, :user_id => @user.id,:is_approved => false, :is_blacklisted => false)
    controller.stub!(:current_user).and_return(@user)
  end
  describe "GET index" do
    it "assigns all merchants as @merchants" do
      merchant = Factory(:merchant,:is_approved => true, :is_blacklisted => false)
      get :index
      assigns(:merchants).should eq([merchant])
    end
  end
end

My merchants_controller.rb

class MerchantsController < ApplicationController

  before_filter :authenticate_user!
  def index
    @merchants = Merchant.approved
    debugger
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @merchants }
    end
  end
end

I have a scope approved in merchant model

scope :approved, where(:is_approved => true, :is_blacklisted => false)

Now my problem is even though i stubbed current_user and returned @user as current_user, My merchants_controller index spec is failing. But if i comment out authenticate_user! then the spec passes,

without authenticate_user! the debugger of index action is caught but with authenticate_user! debugger is not caught.

I think there is problem in subbing current_user and i am not able to figure it out.

Help me out..

like image 570
Gagan Avatar asked Sep 17 '11 03:09

Gagan


1 Answers

Have you read through the docs on github?:

Devise includes some tests helpers for functional specs. To use them, you just need to include Devise::TestHelpers in your test class and use the sign_in and sign_out methods. Such methods have the same signature as in controllers:

sign_in :user, @user   # sign_in(scope, resource)
sign_in @user          # sign_in(resource)

sign_out :user         # sign_out(scope)
sign_out @user         # sign_out(resource)
like image 115
zetetic Avatar answered Oct 13 '22 02:10

zetetic