mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Load definition files instead of require so re-running find_definitions works as expected
Closes #161
This commit is contained in:
parent
1d9a3cf13f
commit
a4e75163c8
4 changed files with 64 additions and 29 deletions
|
@ -3,7 +3,7 @@ Feature: Factory girl can find factory definitions correctly
|
||||||
Given a file named "awesome_factories.rb" with:
|
Given a file named "awesome_factories.rb" with:
|
||||||
"""
|
"""
|
||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
factory :awesome_category, :parent => :category do
|
factory :awesome_category, :class => Category do
|
||||||
name "awesome!!!"
|
name "awesome!!!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,13 +18,13 @@ Feature: Factory girl can find factory definitions correctly
|
||||||
Given a file named "awesome_factories.rb" with:
|
Given a file named "awesome_factories.rb" with:
|
||||||
"""
|
"""
|
||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
factory :awesome_category, :parent => :category do
|
factory :another_awesome_category, :class => Category do
|
||||||
name "awesome!!!"
|
name "awesome!!!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
"""
|
"""
|
||||||
When "awesome_factories.rb" is added to Factory Girl's file definitions path as an absolute path
|
When "awesome_factories.rb" is added to Factory Girl's file definitions path as an absolute path
|
||||||
And I create a "awesome_category" instance from Factory Girl
|
And I create a "another_awesome_category" instance from Factory Girl
|
||||||
Then I should find the following for the last category:
|
Then I should find the following for the last category:
|
||||||
| name |
|
| name |
|
||||||
| awesome!!! |
|
| awesome!!! |
|
||||||
|
@ -33,7 +33,7 @@ Feature: Factory girl can find factory definitions correctly
|
||||||
Given a file named "nested/great_factories.rb" with:
|
Given a file named "nested/great_factories.rb" with:
|
||||||
"""
|
"""
|
||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
factory :great_category, :parent => :category do
|
factory :great_category, :class => Category do
|
||||||
name "great!!!"
|
name "great!!!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -43,3 +43,20 @@ Feature: Factory girl can find factory definitions correctly
|
||||||
Then I should find the following for the last category:
|
Then I should find the following for the last category:
|
||||||
| name |
|
| name |
|
||||||
| great!!! |
|
| great!!! |
|
||||||
|
|
||||||
|
Scenario: Find definitions after clearing loaded factories
|
||||||
|
Given a file named "nested/swell_factories.rb" with:
|
||||||
|
"""
|
||||||
|
FactoryGirl.define do
|
||||||
|
factory :swell_category, :class => Category do
|
||||||
|
name "swell!!!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
When "nested" is added to Factory Girl's file definitions path
|
||||||
|
And I clear out the factories
|
||||||
|
And I find definitions
|
||||||
|
And I create a "swell_category" instance from Factory Girl
|
||||||
|
Then I should find the following for the last category:
|
||||||
|
| name |
|
||||||
|
| swell!!! |
|
||||||
|
|
|
@ -1,16 +1,26 @@
|
||||||
|
module FactoryGirlDefinitionsHelper
|
||||||
|
def append_file_to_factory_girl_definitions_path(path_to_file)
|
||||||
|
FactoryGirl.definition_file_paths ||= []
|
||||||
|
FactoryGirl.definition_file_paths << path_to_file
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
World(FactoryGirlDefinitionsHelper)
|
||||||
|
|
||||||
When /^"([^"]*)" is added to Factory Girl's file definitions path$/ do |file_name|
|
When /^"([^"]*)" is added to Factory Girl's file definitions path$/ do |file_name|
|
||||||
new_factory_file = File.join(current_dir, file_name.gsub(".rb", ""))
|
new_factory_file = File.join(current_dir, file_name.gsub(".rb", ""))
|
||||||
FactoryGirl.definition_file_paths ||= []
|
|
||||||
FactoryGirl.definition_file_paths << new_factory_file
|
append_file_to_factory_girl_definitions_path(new_factory_file)
|
||||||
FactoryGirl.find_definitions
|
|
||||||
|
When %{I find definitions}
|
||||||
end
|
end
|
||||||
|
|
||||||
When /^"([^"]*)" is added to Factory Girl's file definitions path as an absolute path$/ do |file_name|
|
When /^"([^"]*)" is added to Factory Girl's file definitions path as an absolute path$/ do |file_name|
|
||||||
new_factory_file = File.expand_path(File.join(current_dir, file_name.gsub(".rb", "")))
|
new_factory_file = File.expand_path(File.join(current_dir, file_name.gsub(".rb", "")))
|
||||||
|
|
||||||
FactoryGirl.definition_file_paths ||= []
|
append_file_to_factory_girl_definitions_path(new_factory_file)
|
||||||
FactoryGirl.definition_file_paths << new_factory_file
|
|
||||||
FactoryGirl.find_definitions
|
When %{I find definitions}
|
||||||
end
|
end
|
||||||
|
|
||||||
When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name|
|
When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name|
|
||||||
|
@ -23,3 +33,11 @@ Given /^these super users exist:$/ do |table|
|
||||||
new_table = Cucumber::Ast::Table.new([headers] + rows)
|
new_table = Cucumber::Ast::Table.new([headers] + rows)
|
||||||
Given %{the following person exists:}, new_table
|
Given %{the following person exists:}, new_table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
When /^I clear out the factories$/ do
|
||||||
|
FactoryGirl.factories.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
When /^I find definitions$/ do
|
||||||
|
FactoryGirl.find_definitions
|
||||||
|
end
|
||||||
|
|
|
@ -10,14 +10,14 @@ module FactoryGirl
|
||||||
self.definition_file_paths = %w(factories test/factories spec/factories)
|
self.definition_file_paths = %w(factories test/factories spec/factories)
|
||||||
|
|
||||||
def self.find_definitions #:nodoc:
|
def self.find_definitions #:nodoc:
|
||||||
definition_file_paths.each do |path|
|
absolute_definition_file_paths = definition_file_paths.map {|path| File.expand_path(path) }
|
||||||
path = File.expand_path(path)
|
|
||||||
|
|
||||||
require("#{path}.rb") if File.exists?("#{path}.rb")
|
absolute_definition_file_paths.uniq.each do |path|
|
||||||
|
load("#{path}.rb") if File.exists?("#{path}.rb")
|
||||||
|
|
||||||
if File.directory? path
|
if File.directory? path
|
||||||
Dir[File.join(path, '**', '*.rb')].sort.each do |file|
|
Dir[File.join(path, '**', '*.rb')].sort.each do |file|
|
||||||
require file
|
load file
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,20 +2,20 @@ require 'spec_helper'
|
||||||
|
|
||||||
share_examples_for "finds definitions" do
|
share_examples_for "finds definitions" do
|
||||||
before do
|
before do
|
||||||
stub(FactoryGirl).require
|
stub(FactoryGirl).load
|
||||||
FactoryGirl.find_definitions
|
FactoryGirl.find_definitions
|
||||||
end
|
end
|
||||||
subject { FactoryGirl }
|
subject { FactoryGirl }
|
||||||
end
|
end
|
||||||
|
|
||||||
RSpec::Matchers.define :require_definitions_from do |file|
|
RSpec::Matchers.define :load_definitions_from do |file|
|
||||||
match do |given|
|
match do |given|
|
||||||
@has_received = have_received.method_missing(:require, File.expand_path(file))
|
@has_received = have_received.method_missing(:load, File.expand_path(file))
|
||||||
@has_received.matches?(given)
|
@has_received.matches?(given)
|
||||||
end
|
end
|
||||||
|
|
||||||
description do
|
description do
|
||||||
"require definitions from #{file}"
|
"load definitions from #{file}"
|
||||||
end
|
end
|
||||||
|
|
||||||
failure_message_for_should do
|
failure_message_for_should do
|
||||||
|
@ -47,7 +47,7 @@ describe "definition loading" do
|
||||||
describe "with factories.rb" do
|
describe "with factories.rb" do
|
||||||
in_directory_with_files 'factories.rb'
|
in_directory_with_files 'factories.rb'
|
||||||
it_should_behave_like "finds definitions" do
|
it_should_behave_like "finds definitions" do
|
||||||
it { should require_definitions_from('factories.rb') }
|
it { should load_definitions_from('factories.rb') }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -55,14 +55,14 @@ describe "definition loading" do
|
||||||
describe "with a factories file under #{dir}" do
|
describe "with a factories file under #{dir}" do
|
||||||
in_directory_with_files File.join(dir, 'factories.rb')
|
in_directory_with_files File.join(dir, 'factories.rb')
|
||||||
it_should_behave_like "finds definitions" do
|
it_should_behave_like "finds definitions" do
|
||||||
it { should require_definitions_from("#{dir}/factories.rb") }
|
it { should load_definitions_from("#{dir}/factories.rb") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with a factories file under #{dir}/factories" do
|
describe "with a factories file under #{dir}/factories" do
|
||||||
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb')
|
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb')
|
||||||
it_should_behave_like "finds definitions" do
|
it_should_behave_like "finds definitions" do
|
||||||
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,8 +70,8 @@ describe "definition loading" do
|
||||||
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'),
|
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'),
|
||||||
File.join(dir, 'factories', 'person_factory.rb')
|
File.join(dir, 'factories', 'person_factory.rb')
|
||||||
it_should_behave_like "finds definitions" do
|
it_should_behave_like "finds definitions" do
|
||||||
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||||
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/person_factory.rb") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ describe "definition loading" do
|
||||||
File.join(dir, 'factories', 'a.rb')
|
File.join(dir, 'factories', 'a.rb')
|
||||||
it "should load the files in the right order" do
|
it "should load the files in the right order" do
|
||||||
@loaded = []
|
@loaded = []
|
||||||
stub(FactoryGirl).require { |a| @loaded << File.split(a)[-1] }
|
stub(FactoryGirl).load { |a| @loaded << File.split(a)[-1] }
|
||||||
FactoryGirl.find_definitions
|
FactoryGirl.find_definitions
|
||||||
@loaded.should == ["a.rb", "b.rb"]
|
@loaded.should == ["a.rb", "b.rb"]
|
||||||
end
|
end
|
||||||
|
@ -91,9 +91,9 @@ describe "definition loading" do
|
||||||
File.join(dir, 'factories', 'post_factory.rb'),
|
File.join(dir, 'factories', 'post_factory.rb'),
|
||||||
File.join(dir, 'factories', 'person_factory.rb')
|
File.join(dir, 'factories', 'person_factory.rb')
|
||||||
it_should_behave_like "finds definitions" do
|
it_should_behave_like "finds definitions" do
|
||||||
it { should require_definitions_from("#{dir}/factories.rb") }
|
it { should load_definitions_from("#{dir}/factories.rb") }
|
||||||
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||||
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/person_factory.rb") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -101,8 +101,8 @@ describe "definition loading" do
|
||||||
in_directory_with_files File.join(dir, 'factories', 'subdirectory', 'post_factory.rb'),
|
in_directory_with_files File.join(dir, 'factories', 'subdirectory', 'post_factory.rb'),
|
||||||
File.join(dir, 'factories', 'subdirectory', 'person_factory.rb')
|
File.join(dir, 'factories', 'subdirectory', 'person_factory.rb')
|
||||||
it_should_behave_like "finds definitions" do
|
it_should_behave_like "finds definitions" do
|
||||||
it { should require_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
|
||||||
it { should require_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
|
it { should load_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue