In the devise documentation they give tips on how you can have access to current_user when testing a controller:
However, what about when doing a feature test? I am trying to test a create method of one of my controllers, and in that controller is used the current_user variable.
The problem is that the macro suggested in devise uses the @request variable, and it is nil for a feature spec. What is a workaround?
EDIT:
This is what I have so far for my current spec:
feature 'As a user I manage the orders of the system' do
scenario 'User is logged in ad an admin' do
user = create(:user)
order = create(:order, user: user)
visit orders_path
#Expectations
end
end
The problem is that in my OrdersController
I have a current_user.orders
call, and since current_user
is not defined, it will redirect me to /users/sign_in
.
I have defined this under /spec/features/manage_orders.rb
current_user
is there, I think that will help a lot in clarifying your intentions. – Patrick Oscity