Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec not loading support files

I have this file:

# support/auth_macros.rb
module AuthMacros
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @logged_in_user = FactoryGirl.create(:user, username: "logged_in")
      sign_in @logged_in_user
    end
  end

  def logout_user
    before(:each) do
      sign_out @logged_in_user
    end
  end
end

In my spec_helper file, I have this line:

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

Yet when I run rspec, I get errors like:

undefined local variable or method `login_user' for RSpec::ExampleGroups::PostsController::POSTCreate::WhenSignedIn:Class

The relevant function is located in support/auth_macros, which I assume would be picked up by the require statement in my spec_helper.

Any idea what might be going on?

like image 700
nullnullnull Avatar asked Jun 18 '14 15:06

nullnullnull


1 Answers

You have required the file, but the method is wrapped inside a module. You need to either remove the wrapping module or include it within your group test.

Update:

To be 100% specific: require loads the file and do nothing else. After file is required, the module has been created, but it is not included. You need to include it with: include AuthMacros

like image 109
BroiSatse Avatar answered Oct 25 '22 11:10

BroiSatse