# Rspec + Devise + BaseController

**URL:** <https://www.ruby-forum.com/t/rspec-devise-basecontroller/227594>\
**Category:** RSpec\
**Created:** [March 20, 2013, 6:00pm UTC](https://www.ruby-forum.com/t/rspec-devise-basecontroller/227594 "2013-03-20T18:00:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![edelpero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@edelpero](https://www.ruby-forum.com/u/edelpero)\
**Post date:** [March 20, 2013, 6:00pm UTC](https://www.ruby-forum.com/t/rspec-devise-basecontroller/227594/1 "2013-03-20T18:00:12Z")

</div>

Hello there,

I’m creating a base controller for the admin section of a project. All  
controllers whitin the admin section will inherit from it.

#####################################################  
#app/controllers/admins/base\_controller.rb

class Admins::BaseController \< ApplicationController  
layout “admin\_cms”  
before\_filter :authenticate\_admin!  
end

#####################################################

#spec/controllers/admins/base\_controller\_spec.rb

require ‘spec\_helper’

describe Admins::BaseController do  
controller do  
def index  
end  
end

describe “before\_filter#authenticate\_admin!” do  
before(:each) do  
@admin = FactoryGirl.create(:admin)  
@request.env[“devise.mapping”] = Devise.mappings[:admin]  
end

```
context "when admin is not logged in" do
  it "redirect admin to sign_in path" do
    get :index
    response.should redirect_to new_admin_session_path
  end
end

```

end  
end

#####################################################

I’ve already inclueded Devise::TestHelpers on my spec\_helper.rb and I’m  
getting this error when running this spec:

#####################################################

Admins::BaseController  
before\_filter#authenticate\_admin!  
when admin is not logged in  
redirect admin to sign\_in path (FAILED - 1)

Failures:

1. Admins::BaseController before\_filter#authenticate\_admin! when  
admin is not logged in redirect admin to sign\_in path  
Failure/Error: get :index  
ActionView::MissingTemplate:  
Missing template anonymous/index, application/index with  
{:locale=\>[:en], :formats=\>[:html], :handlers=\>[:erb, :builder,  
:coffee]}. Searched in:  
\*  
“#RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0xbaf75d4”

```
 # ./spec/controllers/admins/base_controller_spec.rb:17:in `block (4

```

levels) in \<top (required)\>’

Finished in 0.17124 seconds  
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/admins/base\_controller\_spec.rb:16 #  
Admins::BaseController before\_filter#authenticate\_admin! when admin is  
not logged in redirect admin to sign\_in path

#####################################################

I changed my spec to this:

#####################################################

require ‘spec\_helper’

describe Admins::BaseController do  
controller do  
def index  
render nothing: true  
end  
end

describe “before\_filter#authenticate\_admin!” do  
context “when admin is not logged in” do  
it “redirect admin to sign\_in path” do  
get :index  
response.should redirect\_to new\_admin\_session\_path  
end  
end

end  
end

#####################################################

and now I’m getting this error:

#####################################################

Failures:

1. Admins::BaseController before\_filter#authenticate\_admin! when admin  
is not logged in redirect admin to sign\_in path  
Failure/Error: response.should redirect\_to new\_admin\_session\_path  
Expected response to be a \<:redirect\>, but was \<200\>  
So, for some reason it’s not entering in the authenticate\_admin! before  
filter. I’m kind of lost. Thanks again.

#####################################################

I’m using Rails 3.2.13, Ruby 2.0.0, Rspec-rails 2.13.0 and Devise 2.2.3.  
I really appreacite if someone could help me out with this. Thanks in  
advance.

---

<div class="post-metadata">

**Author:** ![edelpero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@edelpero](https://www.ruby-forum.com/u/edelpero)\
**Post date:** [March 20, 2013, 7:21pm UTC](https://www.ruby-forum.com/t/rspec-devise-basecontroller/227594/2 "2013-03-20T19:21:12Z")

</div>

Finally I could solve the problem. The problem was on the definition of  
the anonymous controller on the spec:

instead of:

```
controller do
  def index
  end
end

```

I used:

```
controller(Admins::Base) do
  def index
  end
end

```

You need to specify always the anonymous controller you’re testing  
unless is ApplicationController the one you’re trying to test.
