Devise adds the #authenticated
and #unauthenticated
constraints to the routing dsl but they rely on request.env['warden']
responding to #authenticate?
. As I understand it, the request doesn't exist in a routing spec so this fails with:
# NoMethodError:
# undefined method `authenticate?' for nil:NilClass
Devise issues #1670, #1779 suggest that this is a limitation but don't suggest how to overcome it.
I decided to try to just stub out authenticate?
but have never done stubbing before so this has been mostly hit-or-miss.
My first attempt looks like this:
require 'spec_helper'
describe 'routes for Tenant' do
it 'routes /organization to TenantsController#show' do
allow_message_expectations_on_nil
allow_any_instance_of(Object).to receive(:authenticate?).and_return(true)
expect(get: '/organization').to route_to(
controller: 'tenants',
action: 'show'
)
end
end
The question: I'm not comfortable stubbing authenticate?
globally nor on NilClass
. How can I stub this ... more generally ... so that I'm not cornered into a specific implementation?
I would like to somehow stub out #authenticated
and #unauthenticated
to simply ignore their implementation and yield
but attempting to do that still results in the original NoMethodError
as if my fake implementation doesn't exist:
expect_any_instance_of(ActionDispatch::Routing::Mapper).to receive(:authenticated) { yield }
expect_any_instance_of(ActionDispatch::Routing::Mapper).to receive(:unauthenticated) { false }
Why does it work the first way but not the second?