mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
80 lines
2.7 KiB
Ruby
80 lines
2.7 KiB
Ruby
|
require 'spec_helper'
|
||
|
|
||
|
share_examples_for "finds definitions" do
|
||
|
before do
|
||
|
stub(FactoryGirl).require
|
||
|
FactoryGirl.find_definitions
|
||
|
end
|
||
|
subject { FactoryGirl }
|
||
|
end
|
||
|
|
||
|
describe "definition loading" do
|
||
|
def self.in_directory_with_files(*files)
|
||
|
before do
|
||
|
@pwd = Dir.pwd
|
||
|
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
|
||
|
FileUtils.mkdir_p @tmp_dir
|
||
|
Dir.chdir(@tmp_dir)
|
||
|
|
||
|
files.each do |file|
|
||
|
FileUtils.mkdir_p File.dirname(file)
|
||
|
FileUtils.touch file
|
||
|
stub(Factory).require(file)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
after do
|
||
|
Dir.chdir(@pwd)
|
||
|
FileUtils.rm_rf(@tmp_dir)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def require_definitions_from(file)
|
||
|
simple_matcher do |given, matcher|
|
||
|
has_received = have_received.method_missing(:require, file)
|
||
|
result = has_received.matches?(given)
|
||
|
matcher.description = "require definitions from #{file}"
|
||
|
matcher.failure_message = has_received.failure_message
|
||
|
result
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe "with factories.rb" do
|
||
|
in_directory_with_files 'factories.rb'
|
||
|
it_should_behave_like "finds definitions"
|
||
|
it { should require_definitions_from('factories.rb') }
|
||
|
end
|
||
|
|
||
|
%w(spec test).each do |dir|
|
||
|
describe "with a factories file under #{dir}" do
|
||
|
in_directory_with_files File.join(dir, 'factories.rb')
|
||
|
it_should_behave_like "finds definitions"
|
||
|
it { should require_definitions_from("#{dir}/factories.rb") }
|
||
|
end
|
||
|
|
||
|
describe "with a factories file under #{dir}/factories" do
|
||
|
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb')
|
||
|
it_should_behave_like "finds definitions"
|
||
|
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
||
|
end
|
||
|
|
||
|
describe "with several factories files under #{dir}/factories" do
|
||
|
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'),
|
||
|
File.join(dir, 'factories', 'person_factory.rb')
|
||
|
it_should_behave_like "finds definitions"
|
||
|
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
||
|
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
||
|
end
|
||
|
|
||
|
describe "with nested and unnested factories files under #{dir}" do
|
||
|
in_directory_with_files File.join(dir, 'factories.rb'),
|
||
|
File.join(dir, 'factories', 'post_factory.rb'),
|
||
|
File.join(dir, 'factories', 'person_factory.rb')
|
||
|
it_should_behave_like "finds definitions"
|
||
|
it { should require_definitions_from("#{dir}/factories.rb") }
|
||
|
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
||
|
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
||
|
end
|
||
|
end
|
||
|
end
|