I am trying to use a helper method in my FactoryBot
file, but when I call require rails_helper
I get a require: cannot load such file -- support/geocoder_helper (LoadError)
.
spec/factories/members.rb
# frozen_string_literal: true
require 'rails_helper'
require 'support/geocoder_helper'
FactoryBot.define do
factory :member do
association :user, roles: ['Member']
cohort
after(:build) do |member, _evaluator|
if member.user
add_geocoder_stub(member.user.full_address_string)
end
end
end
end
spec/support/geocoder_helper.rb
# frozen_string_literal: true
DEFAULT_GEOCODER_STUB_RESULTS = [
{
'coordinates' => [11, -13],
'address' => '123 Main St, Los Angeles, CA, USA',
'state' => 'Los Angeles',
'state_code' => 'CA',
'country' => 'United States',
'country_code' => 'US'
}.freeze
].freeze
def add_geocoder_stub(address, results = DEFAULT_GEOCODER_STUB_RESULTS)
address = User.new(address).full_address_string if address.is_a?(Hash)
Geocoder::Lookup::Test.add_stub(address, results)
end
spec/support/factory_bot.rb
# frozen_string_literal: true
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
spec/rails_helper.rb
# frozen_string_literal: true
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
...
Result:
in `require': cannot load such file -- support/geocoder_helper (LoadError)
This result is the same for all different helpers and all factories.
I am able to use helpers in non FactoryBot files using the same require
patterns.
Versions: rails (5.2.4.6)
rspec (3.9.0)
factory_bot (4.10.0)