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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With