Craig Wanstall
Craig Wanstall

Reputation: 295

Devise and Declarative Authorization Functional testing problems

I am having awful trouble getting functional tests to work when using devise and declarative authorization. I can use the devise test helpers to sign_in a user and then the current_user variable is populated in the controller. But as soon as I use the post_with test helper for declarative authorization the current_user becomes nil.

Can anybody point me at an example of how to write functional tests when using devise and declarative authorization together?

Thanks

Upvotes: 1

Views: 841

Answers (1)

Andreas Richter
Andreas Richter

Reputation: 298

I had the same problem. You can't use declarative authorizations testhelper such as post_with in functional tests with devise.

For functional test you mus use the devise testhelper such as sign_in and sign_out. I've wrote a helper which uses the devise test helper methods for functional tests:

def test_with_user(user, &block)
  begin
    sign_in :user, users(user)
    yield
  ensure
    sign_out :user
  end
end

This helper i use in the following way:

test_with_user(:max) do
  get :new
  assert_response :success
end

Hope that helps.

Upvotes: 1

Related Questions