Skip to content

Commit

Permalink
Load definition files instead of require so re-running find_definitio…
Browse files Browse the repository at this point in the history
…ns works as expected

Closes #161
  • Loading branch information
joshuaclayton committed Jul 29, 2011
1 parent 1d9a3cf commit a4e7516
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
25 changes: 21 additions & 4 deletions features/find_definitions.feature
Expand Up @@ -3,7 +3,7 @@ Feature: Factory girl can find factory definitions correctly
Given a file named "awesome_factories.rb" with:
"""
FactoryGirl.define do
factory :awesome_category, :parent => :category do
factory :awesome_category, :class => Category do
name "awesome!!!"
end
end
Expand All @@ -18,13 +18,13 @@ Feature: Factory girl can find factory definitions correctly
Given a file named "awesome_factories.rb" with:
"""
FactoryGirl.define do
factory :awesome_category, :parent => :category do
factory :another_awesome_category, :class => Category do
name "awesome!!!"
end
end
"""
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:
| name |
| awesome!!! |
Expand All @@ -33,7 +33,7 @@ Feature: Factory girl can find factory definitions correctly
Given a file named "nested/great_factories.rb" with:
"""
FactoryGirl.define do
factory :great_category, :parent => :category do
factory :great_category, :class => Category do
name "great!!!"
end
end
Expand All @@ -43,3 +43,20 @@ Feature: Factory girl can find factory definitions correctly
Then I should find the following for the last category:
| name |
| 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!!! |
30 changes: 24 additions & 6 deletions features/step_definitions/factory_girl_steps.rb
@@ -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|
new_factory_file = File.join(current_dir, file_name.gsub(".rb", ""))
FactoryGirl.definition_file_paths ||= []
FactoryGirl.definition_file_paths << new_factory_file
FactoryGirl.find_definitions

append_file_to_factory_girl_definitions_path(new_factory_file)

When %{I find definitions}
end

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", "")))

FactoryGirl.definition_file_paths ||= []
FactoryGirl.definition_file_paths << new_factory_file
FactoryGirl.find_definitions
append_file_to_factory_girl_definitions_path(new_factory_file)

When %{I find definitions}
end

When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name|
Expand All @@ -23,3 +33,11 @@
new_table = Cucumber::Ast::Table.new([headers] + rows)
Given %{the following person exists:}, new_table
end

When /^I clear out the factories$/ do
FactoryGirl.factories.clear
end

When /^I find definitions$/ do
FactoryGirl.find_definitions
end
8 changes: 4 additions & 4 deletions lib/factory_girl/find_definitions.rb
Expand Up @@ -10,14 +10,14 @@ class << self
self.definition_file_paths = %w(factories test/factories spec/factories)

def self.find_definitions #:nodoc:
definition_file_paths.each do |path|
path = File.expand_path(path)
absolute_definition_file_paths = definition_file_paths.map {|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
Dir[File.join(path, '**', '*.rb')].sort.each do |file|
require file
load file
end
end
end
Expand Down
30 changes: 15 additions & 15 deletions spec/factory_girl/find_definitions_spec.rb
Expand Up @@ -2,20 +2,20 @@

share_examples_for "finds definitions" do
before do
stub(FactoryGirl).require
stub(FactoryGirl).load
FactoryGirl.find_definitions
end
subject { FactoryGirl }
end

RSpec::Matchers.define :require_definitions_from do |file|
RSpec::Matchers.define :load_definitions_from do |file|
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)
end

description do
"require definitions from #{file}"
"load definitions from #{file}"
end

failure_message_for_should do
Expand Down Expand Up @@ -47,31 +47,31 @@ def self.in_directory_with_files(*files)
describe "with factories.rb" do
in_directory_with_files 'factories.rb'
it_should_behave_like "finds definitions" do
it { should require_definitions_from('factories.rb') }
it { should load_definitions_from('factories.rb') }
end
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" do
it { should require_definitions_from("#{dir}/factories.rb") }
it { should load_definitions_from("#{dir}/factories.rb") }
end
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" do
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
end
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" do
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
it { should load_definitions_from("#{dir}/factories/person_factory.rb") }
end
end

Expand All @@ -80,7 +80,7 @@ def self.in_directory_with_files(*files)
File.join(dir, 'factories', 'a.rb')
it "should load the files in the right order" do
@loaded = []
stub(FactoryGirl).require { |a| @loaded << File.split(a)[-1] }
stub(FactoryGirl).load { |a| @loaded << File.split(a)[-1] }
FactoryGirl.find_definitions
@loaded.should == ["a.rb", "b.rb"]
end
Expand All @@ -91,18 +91,18 @@ def self.in_directory_with_files(*files)
File.join(dir, 'factories', 'post_factory.rb'),
File.join(dir, 'factories', 'person_factory.rb')
it_should_behave_like "finds definitions" do
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") }
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") }
end
end

describe "with deeply nested factory files under #{dir}" do
in_directory_with_files File.join(dir, 'factories', 'subdirectory', 'post_factory.rb'),
File.join(dir, 'factories', 'subdirectory', 'person_factory.rb')
it_should_behave_like "finds definitions" do
it { should require_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/post_factory.rb") }
it { should load_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
end
end
end
Expand Down

0 comments on commit a4e7516

Please sign in to comment.