RSpec controller macro does not work

Rails 3.2, RSpec 2.11. The controller macro does not work, and it seems to be written correctly from all my research. Here is 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)

+6
source share
2 answers

It seems the answer was given here , you need to change extend to include

+2
source

Adding type specs fixed for me:

Before:

 describe Api::FooController do . . end 

After:

 describe Api::FooController, type: :controller do . . end 
+1
source

All Articles