How to access (devise) current_user in a rspec feature test?
Asked Answered
L

3

28

In the devise documentation they give tips on how you can have access to current_user when testing a controller:

https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

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

Linnell answered 21/5, 2014 at 20:54 Comment(4)
I've used github.com/railsware/rack_session_access for something like thatGiggle
Also, see blog.pixarea.com/2013/01/… and #5866055.Aboutface
Maybe include the test code you are trying to write? You can just pretend that current_user is there, I think that will help a lot in clarifying your intentions.Dopp
I am not logging the user because I don't know how to do that in a feature rspec test.Linnell
G
35

from https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

if i have understood you right, maybe you need to use

subject.current_user.email
#or
controller.current_user.email

for example :

describe OrdersController, :type => :controller do
  login_user

  describe "POST 'create'" do
     it "with valid parametres" do
        post 'create', title: 'example order', email: subject.current_user.email
     end
  end
end

controller_macros.rb :

module ControllerMacros
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryGirl.create(:user)
      #user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
      sign_in user
    end
  end
end

Don't forget to include this into your spec_helper.rb :

config.include Devise::TestHelpers, type: :controller
config.extend ControllerMacros, type: :controller
Glycoprotein answered 20/1, 2015 at 14:26 Comment(5)
Simply this worked for me: subject.current_user.emailCalvities
where to put the controller_macros.rb ? In spec/support folder ?Incompressible
@Incompressible yes.Glycoprotein
@Hommer Smith this is the correct answer. I tested it too and it works. If it is accepted it will show up on the search query as solved.Print
This answer along with that devise wiki page is for controller specs. The question was about feature specs; and devise has a separate page for that: github.com/plataformatec/devise/wiki/How-To:-Test-with-CapybaraOdoacer
G
2

Here's what I think you are looking for:

require 'spec_helper'
include Warden::Test::Helpers
Warden.test_mode!

feature 'As a user I manage the orders of the system' do
  scenario 'User is logged in ad an admin' do
    user = create(:user)
    login_as(user, scope: :user)
    order = create(:order, user: user)
    visit orders_path
    #Expectations
  end
end
Gillmore answered 30/11, 2014 at 5:31 Comment(1)
This is the most correct answer so far as it is very close to the officially recommended way on the devise wiki for feature specs (not controller specs) github.com/plataformatec/devise/wiki/How-To:-Test-with-CapybaraOdoacer
P
2

you can define login_user as a method for the user to login as follows (put it in support folder):

def login_user
  Warden.test_mode!
  user = create(:user)
  login_as user, :scope => :user
  user.confirmed_at = Time.now
  user.confirm!
  user.save
  user
end

Then in the scenario say:

user = login_user
Payroll answered 30/11, 2014 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.