Using Devise in rspec feature tests - ruby-on-rails

I've written the following rspec feature test spec:
require "rails_helper"
RSpec.describe "Team management", type: :feature do
user_sign_in
describe "User creates a new team" do
...
expect(page).to have_link("#{team_name}")
end
end
The user_sign_in method is defined in my rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/rails'
...
module UserSignInHelpers
def user_sign_in
before(:each) do
#request.env['devise.mapping'] = Devise.mappings[:user]
#current_user = FactoryGirl.create(:user)
#current_user.confirm
sign_in :user, #current_user
end
end
end
RSpec.configure do |config|
...
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
config.extend UserSignInHelpers, type: :controller
config.extend UserSignInHelpers, type: :feature
config.include Devise::TestHelpers, type: :controller
config.include Devise::TestHelpers, type: :feature
end
The user_sign_in method works from all of my controller specs but when I run my feature spec it fails with:
Team management
User creates a new team
example at ./spec/features/user_creates_a_new_team_spec.rb:19 (FAILED - 1)
Failures:
1) Team management User creates a new team
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /Users/xxxx/.rvm/gems/ruby-2.2.1/gems/devise-3.5.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
I don't understand why this works in controller tests and not feature tests. Is there something I can do make this work in feature tests?

There is a basic problem with what you are trying to do. (Devise work on top of Warden)Warden is a
Rack middleware, but RSpec controller specs don't even include Rack,
as these types of specs are not meant to run your full application
stack, but only your controller code.
Ref
Test with Capybara
I have a simple support helper, that allows me to login and logout users:
module Auth
def create_user!
#user = User.create(email: 'foo#bar.com', password: '11111111')
end
def sign_in_user!
setup_devise_mapping!
sign_in #user
end
def sign_out_user!
setup_devise_mapping!
sign_out #user
end
def setup_devise_mapping!
#request.env["devise.mapping"] = Devise.mappings[:user]
end
def login_with_warden!
login_as(#user, scope: :user)
end
def logout_with_warden!
logout(:user)
end
def login_and_logout_with_devise
sign_in_user!
yield
sign_out_user!
end
def login_and_logout_with_warden
Warden.test_mode!
login_with_warden!
yield
logout_with_warden!
Warden.test_reset!
end
end
in a feature:
RSpec.describe "Team management", type: :feature do
describe "User creates a new team" do
login_and_logout_with_warden do
# tests goes here
end
end
end
in a controller:
RSpec.describe "Team management", type: :controller do
describe "User creates a new team" do
login_and_logout_with_devise do
# tests goes here
end
end
end

Related

Why is my RSpec not loading Devise::Test::ControllerHelpers?

I'm using Rails 5, and Devise 3.5.1.
Going through a nice (older) book about creating/testing an API, which uses Devise authentication. It was written before Rails 5, so I chose not to use the new api-only version.
Here's my test...
#/spec/controllers/api/v1/users_controller_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, :type => :controller do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
#user = FactoryGirl.create :user
get :show, params: {id: #user.id}, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql #user.email
end
it { should respond_with 200 }
end
end
And here's a completely unexpected RSpec error
Devise::MissingWarden:
Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.
So I go here - http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers
and tried this -> include Devise::Test::ControllerHelpers
which didn't help because the file controller_helpers.rb is nowhere in my project
What did I miss here?
Thanks
You could add the following to your rails_helper:
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
This will include Devise::Test::ControllerHelpers module in all :controller specs.
In the spec_helper.rb add:
config.include Devise::Test::ControllerHelpers, :type => :controller
MiniTest, Rails 4
This works for vanilla Rails 4 MiniTest
test\controllers\users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
include Warden::Test::Helpers
include Devise::Test::ControllerHelpers
setup do
# https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
# #user = users(:admin)
# sign_in #user
end
teardown do
Warden.test_reset!
end
test "login as admin" do
#user = users :admin
sign_in #user
get :dashboard
assert_redirected_to admin_dashboard_path
end
end

Adding helper module to Rspec

I would like to add a module that includes a method to help me log in as a user. Here it is:
module TestHelper
require 'spec_helper'
ALL_USERS = ["administrator", "instructor", "regular_user"]
def self.login_as(user_type)
user = User.find_by(global_role_id: GlobalRole.send(user_type))
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in user
end
end
The spec that's calling it is
require 'spec_helper'
RSpec.describe QuestionsController, :type => :controller do
include Devise::TestHelpers
include TestHelper
describe "a test" do
it "works!" do
TestHelper.login_as("administrator")
end
end
end
And here is the spec_helper
RSpec.configure do |config|
config.include TestHelper, :type => :controller
The error I get is: undefined method 'env' for nil:NilClass It looks like my module doesn't have access to #request.
My question is: how do I access and #request in the external Module?
In addition to the other answers, you should consider only including this module for relevant spec types using the following code:
config.include TestHelper, type: :controller # Or whatever spec type(s) you're using
You could pass #request in to TestHelper.login_as from the tests. Eg
module TestHelper
def self.login_as(user_type, request)
...
request.env['devise.mapping'] = Devise.mappings[:user]
...
end
end
...
describe 'log in' do
it 'works!' do
TestHelper.login_as('administrator', #request)
end
end
But probably better to follow the macro pattern from the devise wiki if other devise mappings in play.

Trouble including controller macros for rspec tests /w devise

I am getting a uninitialized constant ControllerMacros (NameError), perhaps similar to these issues (1, 2, 3). I must be screwing up the syntax while trying to include controller macros so I can login with devise and pass controller tests in rspec. Link to GitHub repo.
Rails 4.1.8 and Ruby 2.1.2
spec/controllers/static_pages_controller_spec.rb
require 'rails_helper'
describe StaticPagesController, :type => :controller do
describe "GET #index" do
it "responds successfully with an HTTP 200 status code" do
login_user
get :index
expect(response).to be_success
expect(response).to have_http_status(200)
end
it "renders the index template" do
login_user
get :root
expect(response).to render_template("index")
end
end
end
spec/support/controller_macros.rb
module ControllerMacros
def login_admin
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:admin]
admin = FactoryGirl.create(:admin)
sign_in :user, admin # sign_in(scope, resource)
end
end
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
added lines to spec/rails_helper
#helps avoid authentication error during rspec:
config.include Devise::TestHelpers, :type => :controller
config.include ControllerMacros, :type => :controller
This worked for me.
spec/support/devise.rb
require 'devise'
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
end
Also make sure this line is uncommented in rails_helper.rb
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
I looks like you may need to add require 'support/controller_macros' to the top of your rails_helper.rb file. This directory would not be included by default with RSpec.

rspec fails where 'current_user' code written in controller

I have started rspec coding recently and i am new to rails framework, rspec fails where i am using 'current_user' in controller. Please check below for my controller and rspec code. Thanks in advance.
Controller code:
def task
#tasks = current_user.alerts.where(kind: "TASK")
end
rspec code:
describe "get #task" do
it "assigns a task" do
sign_in(#user)
get :task
expect(response).to have_http_status(200)
end
You can do it like this:
#request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user) # Don't forget to create a factory for user
user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
sign_in user
It is better to put it in support/devise.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
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
end
you can say login_user instead of sign_in(#user)

RSpec controller macro not working

Rails 3.2, RSpec 2.11. Controller macro isn't working, and it appears to be written correctly from all the research I've done. Here's the code
/spec/support/controller_macros.rb
module ControllerMacros
def login_user
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
#current_user = user
sign_in user
end
end
end
/spec/spec_helper.rb
RSpec.configure do |config|
....
config.extend ControllerMacros, :type => :controller
end
/spec/controllers/companies_controller_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
describe CompaniesController, "index companies" do
context "for authenticated users" do
login_user
...
end
end
execution results:
undefined local variable or method 'login_user' for # (NameError)
Seems to have been answered here , you need to change your extend to an include
Adding the spec type fixed it for me:
Before:
describe Api::FooController do
.
.
end
After:
describe Api::FooController, type: :controller do
.
.
end

Resources