测试 cancan 与 integration test

aisensiy · January 28, 2013 · Last by aisensiy replied at January 28, 2013 · 2760 hits

想要写个 integration_test 去测试 cancan 配置的东西。比如测试没有登录不能 create,或者是只有自己的 post 才可以 update。应个怎么写这个测试呢?我在没有登录的情况下做

get "/posts/new"
assert_raise CanCan::AccessDenied

测试依然报错

CanCan::AccessDenied: You are not authorized to access this page.

应当怎么做好呢

无责任猜测一下😄

assert_raise CanCan::AccessDenied do
 get "/posts/new"
end

#1 楼 @keating 就是这样,谢谢!

@keating 在麻烦看下 code,我用 cancan 设置了权限,可是为什么非作者的用户依然可以修改任意的 post 呢...

@aisensiy 是因为在authorize_resource之前,先要load_resource,或者直接load_and_authorize_resource?不知道是否正确,看的这里

#5 楼 @keating 哇,确实是这样,thx~

You need to Sign in before reply, if you don't have an account, please Sign up first.