Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 941
justindfunk Avatar asked Mar 18 '13 14:03

justindfunk


2 Answers

Seems to have been answered here , you need to change your extend to an include

like image 96
omnikron Avatar answered Sep 30 '22 08:09

omnikron


Adding the spec type fixed it for me:

Before:

describe Api::FooController do
   .
   .
end

After:

describe Api::FooController, type: :controller do
   .
   .
end
like image 34
Charlie Avatar answered Sep 30 '22 07:09

Charlie