current_user returns TrueClass instead of a User in a controller using Devise
-
27-09-2019 - |
Question
I'm using Devise 1.0.8 on a Rails 2.3.8 project. In my controller test I have:
context "when current_user is admin" do
should "render" do
sign_in Factory(:admin)
get :index
assert_response :success
end
end
And in the controller itself, there's the following before_filter:
def redirect_if_not_admin
puts current_user
redirect_to root_path unless current_user.try(:admin?)
end
This is the output from my test:
true
false
NoMethodError: undefined method `admin?' for true:TrueClass
So current_user is true when I use sign_in in the test. However when I don't, current_user is nil, so it's definitely being affected. Thanks in advance for any help.
Solution
I used the confirm! method on the resource in an after_create/build instead of setting the attributes in the factory and that did the trick. current_user equals true when there is an unconfirmed user on the site (someone who has signed up but has yet to click on the confirmation link sent to them).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow