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 7571227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:37:26+00:00 2026-05-30T15:37:26+00:00

I am trying to test a CanCan ability in my app that also uses

  • 0

I am trying to test a CanCan ability in my app that also uses Authlogic. I have verified the correct behavior works when using the actual site, but I want to write a functional test that will alert me if this behavior breaks in the future. My ability file is simple, and looks as follows:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    can :read, User
    can :manage, User, :id => user.id
    cannot :create, User
    can :destroy, UserSession

    if user.role? :guest
      can :create, UserSession
      cannot :destroy UserSession
    end
  end
end

My test for the UserSessionsController is also simple, and looks like this:

test "should redirect new for member" do
  default_user = login :default_user

  assert default_user.role? :member
  assert_raise(CanCan::AccessDenied) { get :new }
  assert_redirected_to root_path
end

Just for reference, my test_helper.rb looks like this:

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'authlogic/test_case'

class ActiveSupport::TestCase
  fixtures :all
  setup :activate_authlogic

  def login(user_login)
    UserSession.create users(user_login)
    users(user_login)
  end
end

When I run my code, my test fails, however:

test_should_redirect_new_for_member                                  FAIL
        CanCan::AccessDenied expected but nothing was raised.
        Assertion at test/functional/user_sessions_controller_test.rb:13:in `block in <class:UserSessionsControllerTest>'

If I comment out the assert_raise, the redirect assertion also fails. Does anyone see anything wrong with my code that is causing this test to fail?

  • 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-30T15:37:27+00:00Added an answer on May 30, 2026 at 3:37 pm

    The problem was that I was rescuing the AccessDenied in my ApplicationController, so the exception was never being raised.

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

Sidebar

Related Questions

I trying to test an AccountController that uses DotNetOpenAuth but I am running into
Im trying to test my successfully creates a new user after login (using authlogic).
Trying to test deploy a simple Rails 3.1 app in production mode, using the
I'm trying to test a utility method I have that creates urlencoded query strings.
I'm trying to test web service calls using an ASP.NET page that creates a
I am trying to test the likelihood that a particular clustering of data has
I'm trying to test the functionality of a web app by scripting a login
I am trying to test that a particular method throws an expected exception from
I am trying to test a class that manages data access in the database
I am trying to test a proof of concept that I can run a

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.