2015-09-11 09:16:23 -04:00
|
|
|
shared_examples_for "finds definitions" do
|
2010-06-24 09:45:57 -04:00
|
|
|
before do
|
2017-10-20 15:20:28 -04:00
|
|
|
allow(FactoryBot).to receive(:load)
|
|
|
|
FactoryBot.find_definitions
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2011-08-13 01:03:12 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot }
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
|
2011-07-29 12:22:29 -04:00
|
|
|
RSpec::Matchers.define :load_definitions_from do |file|
|
2010-11-11 16:33:45 -05:00
|
|
|
match do |given|
|
2011-08-12 22:06:10 -04:00
|
|
|
@has_received = have_received(:load).with(File.expand_path(file))
|
2010-11-11 16:33:45 -05:00
|
|
|
@has_received.matches?(given)
|
|
|
|
end
|
|
|
|
|
|
|
|
description do
|
2011-07-29 12:22:29 -04:00
|
|
|
"load definitions from #{file}"
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
|
|
|
|
2015-09-11 09:16:23 -04:00
|
|
|
failure_message do
|
2010-11-11 16:33:45 -05:00
|
|
|
@has_received.failure_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe "definition loading" do
|
|
|
|
def self.in_directory_with_files(*files)
|
|
|
|
before do
|
|
|
|
@pwd = Dir.pwd
|
2018-10-07 21:45:51 -04:00
|
|
|
@tmp_dir = File.join(File.dirname(__FILE__), "tmp")
|
2010-06-24 09:45:57 -04:00
|
|
|
FileUtils.mkdir_p @tmp_dir
|
|
|
|
Dir.chdir(@tmp_dir)
|
|
|
|
|
|
|
|
files.each do |file|
|
|
|
|
FileUtils.mkdir_p File.dirname(file)
|
|
|
|
FileUtils.touch file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Dir.chdir(@pwd)
|
|
|
|
FileUtils.rm_rf(@tmp_dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with factories.rb" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files "factories.rb"
|
2010-11-11 16:33:45 -05:00
|
|
|
it_should_behave_like "finds definitions" do
|
2018-10-07 21:45:51 -04:00
|
|
|
it { should load_definitions_from("factories.rb") }
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
%w[spec test].each do |dir|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe "with a factories file under #{dir}" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files File.join(dir, "factories.rb")
|
2010-11-11 16:33:45 -05:00
|
|
|
it_should_behave_like "finds definitions" do
|
2011-07-29 12:22:29 -04:00
|
|
|
it { should load_definitions_from("#{dir}/factories.rb") }
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a factories file under #{dir}/factories" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files File.join(dir, "factories", "post_factory.rb")
|
2010-11-11 16:33:45 -05:00
|
|
|
it_should_behave_like "finds definitions" do
|
2011-07-29 12:22:29 -04:00
|
|
|
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "with several factories files under #{dir}/factories" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files File.join(dir, "factories", "post_factory.rb"),
|
2020-06-05 15:15:18 -04:00
|
|
|
File.join(dir, "factories", "person_factory.rb")
|
2010-11-11 16:33:45 -05:00
|
|
|
it_should_behave_like "finds definitions" do
|
2011-07-29 12:22:29 -04:00
|
|
|
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
|
|
|
it { should load_definitions_from("#{dir}/factories/person_factory.rb") }
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
|
2010-08-10 16:54:15 -04:00
|
|
|
describe "with several factories files under #{dir}/factories in non-alphabetical order" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files File.join(dir, "factories", "b.rb"),
|
2020-06-05 15:15:18 -04:00
|
|
|
File.join(dir, "factories", "a.rb")
|
2011-10-12 14:32:23 -04:00
|
|
|
it "loads the files in the right order" do
|
2017-10-20 15:20:28 -04:00
|
|
|
allow(FactoryBot).to receive(:load)
|
2017-06-01 12:54:02 -04:00
|
|
|
wd = File.dirname(__FILE__)
|
|
|
|
file_b = File.join(wd, "tmp", dir, "factories", "b.rb")
|
|
|
|
file_a = File.join(wd, "tmp", dir, "factories", "a.rb")
|
2017-09-28 09:30:17 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.find_definitions
|
2017-09-28 09:30:17 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot).to have_received(:load).with(file_a).ordered
|
|
|
|
expect(FactoryBot).to have_received(:load).with(file_b).ordered
|
2010-08-10 16:54:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe "with nested and unnested factories files under #{dir}" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files File.join(dir, "factories.rb"),
|
2020-06-05 15:15:18 -04:00
|
|
|
File.join(dir, "factories", "post_factory.rb"),
|
|
|
|
File.join(dir, "factories", "person_factory.rb")
|
2010-11-11 16:33:45 -05:00
|
|
|
it_should_behave_like "finds definitions" do
|
2011-07-29 12:22:29 -04:00
|
|
|
it { should load_definitions_from("#{dir}/factories.rb") }
|
|
|
|
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
|
|
|
it { should load_definitions_from("#{dir}/factories/person_factory.rb") }
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2011-06-27 15:24:08 -04:00
|
|
|
|
|
|
|
describe "with deeply nested factory files under #{dir}" do
|
2018-10-07 21:45:51 -04:00
|
|
|
in_directory_with_files File.join(dir, "factories", "subdirectory", "post_factory.rb"),
|
2020-06-05 15:15:18 -04:00
|
|
|
File.join(dir, "factories", "subdirectory", "person_factory.rb")
|
2011-06-27 15:24:08 -04:00
|
|
|
it_should_behave_like "finds definitions" do
|
2011-07-29 12:22:29 -04:00
|
|
|
it { should load_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
|
|
|
|
it { should load_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
|
2011-06-27 15:24:08 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
end
|