Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:32:19+00:00 2026-05-28T23:32:19+00:00

If I have a Devise model User, of which only those users with role

  • 0

If I have a Devise model User, of which only those users with role :admin are allowed to view a certain url, how can I write an RSpec integration test to check that the status returns 200 for that url?

def login(user)
  post user_session_path, :email => user.email, :password => 'password'
end

This was pseudo-suggested in the answer to this question: Stubbing authentication in request spec, but I can’t for the life of me get it to work with devise. CanCan is receiving a nil User when checking Ability, which doesn’t have the correct permissions, naturally.

There’s no access to the controller in integration specs, so I can’t stub current_user, but I’d like to do something like this.

describe "GET /users" do
  it "should be able to get" do
    clear_users_and_add_admin #does what it says...
    login(admin)
    get users_path
    response.status.should be(200)
  end
end

NOTE!!!: all this has changed since the question was asked. The current best way to do this is here: http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:32:20+00:00Added an answer on May 28, 2026 at 11:32 pm

    @pschuegr’s own answer got me across the line. For completeness, this is what I did that gets me easily set up for both request specs and controller specs (using FactoryGirl for creating the user instance):

    in /spec/support/sign_in_support.rb:

    #module for helping controller specs
    module ValidUserHelper
      def signed_in_as_a_valid_user
        @user ||= FactoryGirl.create :user
        sign_in @user # method from devise:TestHelpers
      end
    end
    
    # module for helping request specs
    module ValidUserRequestHelper
    
      # for use in request specs
      def sign_in_as_a_valid_user
        @user ||= FactoryGirl.create :user
        post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
      end
    end
    
    RSpec.configure do |config|
      config.include ValidUserHelper, :type => :controller
      config.include ValidUserRequestHelper, :type => :request
    end
    

    Then in request spec:

    describe "GET /things" do
      it "test access to things, works with a signed in user" do
        sign_in_as_a_valid_user
        get things_path
        response.status.should be(200)
      end
    end
    
    describe "GET /things" do
      it "test access to things, does not work without a signed in user" do
        get things_path
        response.status.should be(302) # redirect to sign in page
      end
    end
    

    and similarly, use ‘signed_in_as_valid_user’ in controller specs (which wraps Devise::TestHelpers sign_in method with a user from FactoryGirl)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Devise for authentication and I have an admin role who can manage
I'm building an app in Rails 3. I currently have a user model which
I am using devise for my user model and in the routes.rb I have
I have one model User with Devise gem. I added a new field called
I have a very simple issue: User model: class User < ActiveRecord::Base devise :database_authenticatable,
I have a Post model in my app, which belongs_to a User model, as
I have the same issue as Creating an additional related model with Devise (which
HI, I have a device that exposes a telnet interface which you can log
I have a user model whose access controlled by ACL9. in UsersController : #ACL9
I have a web app which uses Devise for authentication. It is a site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.