Closed jrochkind closed 1 month ago
Ah, I see the sign_in
method I am using in test setup in a decorator test, that I thought was coming from Devise::Test::ControllerHelpers
, is actually coming from this gem, Draper::DeviseHelper
.
Not sure what's going on, or how this changes things. But looking at what that does, to see if I can figure out how to make it set up a nil
current_user instead... and if there's a way to do this in a global before
block for decorator specs, that can still be overrridden by a specific sign_in
.
This seems to work if I put it in my rails_helper.rb
:
config.before(:each, type: :decorator) do
_stub_current_scope :user, nil
end
Can't actually use the Draper::DeviseHelper#sign_in
, because of the way nil
short-circuits it, so do have to use the underscored (presumably not meant for public consumption) Draper::DeviseHelpers#_stub_current_user_scope
.
This will of course define a singleton method current_user on the fly.
If I then have some tests that do want a non-nil current_user, and call sign_in
with an argument... that will define a current_user
on the singleton again, on top of the first one.
This does seem to work... but is all a bit hacky.
Should Draper document this better and/or support this better? It seems reasonable to want devise current_user to work for nil user in decorator tests, not just for when a particular user has been mocked as logged in with sign_in
.
Did you try sign_out
? That should work:
My decorators sometimes need to access the
current_user?
helper, from devise.If I mock a current user using Devise::Test::ControllerHelpers sign_in, like so:
That works, no problem. My decorators that access current_user get back that specified user when run under the spec, all is good.
But my app has public facing pages that have no signed in user, current_user is nil.
If I run a spec that has not done a Devise::Test::ControllerHelpers
sign_in
with a user -- because I am trying to test the scenario where there is no logged_in user, current_user is nil -- and that spec tries to exersize a decorator which asks forcurrent_user
, I get the dreaded:Very simple reproduction (Devise 4.6.2, Draper 3.1.0, Rails 5.2.3):
That will raise the Devise::MissingWarden
Is there a way to set up my :decorator specs so
h.current_user
works, even when I am not mocking a logged in user, I want no logged in user?Ideally it would be something I could set up globally that would Just Work, and then specs which do want a logged in user would use Devise::Test::ControllerHelpers#sign_in as ordinary. If I need to do completely different mutually exclusive set up for "a mocked logged in user" vs "no mocked logged in user, current_user should be nil", that would be unfortunate. But would still at least get my tests possible again!