In GETTING_STARTED:
Factories can be defined anywhere, but will be automatically loaded if they are defined in files at the following locations: ...
Under 4.5.0 this is not true, the provided paths are in FactoryGirl.definition_file_paths, but one must explicitly call FactoryGirl.find_definitions for them to be found. Otherwise, a Factory not registered error will be raised.
Small example:
# spec/spec_helper
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# spec/foo_spec.rb
require "spec_helper"
RSpec.describe Foo do
describe "#bar" do
it "does some thangz" do
# Uncomment this to work
# FactoryGirl.find_definitions
p FactoryGirl.definition_file_paths
p FactoryGirl.factories
foo = build(:foo)
end
end
end
Note that this is with Bundler, though I'm also using Padrino so...
As a workaround, putting FactoryGirl.find_definitions in the configure block seems to work:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before do
FactoryGirl.find_definitions
end
end
I had this same problem did the workaround suggested by @dmolesUC3 and found that it would work for the first spec then fail on the next because of duplicate definitions. My workaround:
FactoryGirl.find_definitions if FactoryGirl.factories.count == 0
Will try see if I can find the issue in the Factory Girl source. Edit: No luck had a pretty good look will probably need some help from a maintainer.
Can confirm my bundler is setup and working.
@sshaw is there wording that would improve the experience here? The documentation is suggesting that, when find_definitions is called, it'll look in FactoryGirl.definition_file_paths.
I'd like to note that FactoryGirl Rails will manage loading files for you if you're testing in Rails.
@sshaw is there wording that would improve the experience here? The documentation is suggesting ?> that, when find_definitions is called, it'll look in FactoryGirl.definition_file_paths.
I took the docs to mean that it's not be necessary to call find_definitions if factories are created in the given locations.
If indeed calling find_definitions is required then it's better to say:
"Factories can be defined anywhere, but find_definitions will only load factories defined in files at the following locations"
@joshuaclayton As another data point, I also I ran into this issue as well attempting to use FactoryGirl with Bundler and RSpec on a Ruby project - the wording in the GETTING_STARTED doc could definitely use some refinement as it led me to believe initially that the four specified paths were default factory resolution paths and there was no need to explicitly invoke anything to get factory_girl to resolve my project factories.
In fact, FactoryGirl.find_definitions isn't even mentioned except late in the document in the context of a non-bundler project. Perhaps explicitly calling out the need to invoke find_definitions would improve the experience here?
Closing based on the resolution of #839
Most helpful comment
I had this same problem did the workaround suggested by @dmolesUC3 and found that it would work for the first spec then fail on the next because of duplicate definitions. My workaround:
Will try see if I can find the issue in the Factory Girl source. Edit: No luck had a pretty good look will probably need some help from a maintainer.
Can confirm my bundler is setup and working.