【问题标题】:Devise and Declarative Authorization Functional testing problems设计和声明式授权功能测试问题
【发布时间】:2011-01-09 21:14:04
【问题描述】:
在使用设计和声明式授权时,我很难让功能测试正常工作。我可以使用设计测试助手来登录用户,然后将 current_user 变量填充到控制器中。但是一旦我使用 post_with 测试助手进行声明式授权, current_user 就会变为 nil。
谁能给我举个例子,说明在同时使用设计和声明式授权时如何编写功能测试?
谢谢
【问题讨论】:
标签:
ruby-on-rails
testing
devise
declarative-authorization
【解决方案1】:
我遇到了同样的问题。
您不能在带有 devise 的功能测试中使用声明性授权 testhelper,例如 post_with。
对于功能测试,您必须使用设计测试助手,例如 sign_in 和 sign_out。我编写了一个帮助器,它使用设计测试帮助器方法进行功能测试:
def test_with_user(user, &block)
begin
sign_in :user, users(user)
yield
ensure
sign_out :user
end
end
我使用这个助手的方式如下:
test_with_user(:max) do
get :new
assert_response :success
end
希望对您有所帮助。