1

在使用设计和声明式授权时,我很难让功能测试正常工作。我可以使用设计测试助手来登录用户,然后将 current_user 变量填充到控制器中。但是,一旦我使用 post_with 测试助手进行声明性授权, current_user 就会变为 nil。

任何人都可以举一个例子,说明在同时使用设计和声明性授权时如何编写功能测试?

谢谢

4

1 回答 1

1

我有同样的问题。您不能在带有设计的功能测试中使用声明性授权 testhelper,例如 post_with。

对于功能测试,您必须使用设计 testhelper,例如 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

希望有帮助。

于 2011-01-10T20:28:45.330 回答