我想在我的應用中使用Authlogic測試CanCan能力。我已經驗證了使用實際站點時的正確行爲,但我想編寫一個功能測試,以便在將來此行爲發生中斷時提醒我。我的能力文件是簡單,看起來如下:CanCan能力失敗的功能測試
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
我對UserSessionsController測試也很簡單,看起來像這樣:
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
僅供參考,我test_helper.rb中看起來像這樣:
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
當我運行我的代碼,我的測試失敗,但是,:
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>'
如果我註釋掉assert_raise,重定向斷言也失敗。有沒有人看到我的代碼導致此測試失敗的任何錯誤?
你的控制器在哪裏? – Zabba