mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Fix find_definitions to expand the path instead of always prepending .
This commit is contained in:
parent
d175bf4496
commit
17f3ef408d
4 changed files with 38 additions and 13 deletions
|
@ -14,6 +14,21 @@ Feature: Factory girl can find factory definitions correctly
|
||||||
| name |
|
| name |
|
||||||
| awesome!!! |
|
| awesome!!! |
|
||||||
|
|
||||||
|
Scenario: Find definitions with an absolute path
|
||||||
|
Given a file named "awesome_factories.rb" with:
|
||||||
|
"""
|
||||||
|
FactoryGirl.define do
|
||||||
|
factory :awesome_category, :parent => :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
|
||||||
|
Then I should find the following for the last category:
|
||||||
|
| name |
|
||||||
|
| awesome!!! |
|
||||||
|
|
||||||
Scenario: Find definitions with a folder
|
Scenario: Find definitions with a folder
|
||||||
Given a file named "nested/great_factories.rb" with:
|
Given a file named "nested/great_factories.rb" with:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -5,6 +5,14 @@ When /^"([^"]*)" is added to Factory Girl's file definitions path$/ do |file_nam
|
||||||
FactoryGirl.find_definitions
|
FactoryGirl.find_definitions
|
||||||
end
|
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
|
||||||
|
end
|
||||||
|
|
||||||
When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name|
|
When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name|
|
||||||
FactoryGirl.create(factory_name)
|
FactoryGirl.create(factory_name)
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,11 +11,13 @@ module FactoryGirl
|
||||||
|
|
||||||
def self.find_definitions #:nodoc:
|
def self.find_definitions #:nodoc:
|
||||||
definition_file_paths.each do |path|
|
definition_file_paths.each do |path|
|
||||||
require("./#{path}.rb") if File.exists?("#{path}.rb")
|
path = File.expand_path(path)
|
||||||
|
|
||||||
|
require("#{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}"
|
require file
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@ end
|
||||||
|
|
||||||
RSpec::Matchers.define :require_definitions_from do |file|
|
RSpec::Matchers.define :require_definitions_from do |file|
|
||||||
match do |given|
|
match do |given|
|
||||||
@has_received = have_received.method_missing(:require, file)
|
@has_received = have_received.method_missing(:require, File.expand_path(file))
|
||||||
@has_received.matches?(given)
|
@has_received.matches?(given)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -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 require_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 require_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 require_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 require_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||||
it { should require_definitions_from("./#{dir}/factories/person_factory.rb") }
|
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
||||||
end
|
end
|
||||||
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 require_definitions_from("#{dir}/factories.rb") }
|
||||||
it { should require_definitions_from("./#{dir}/factories/post_factory.rb") }
|
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||||
it { should require_definitions_from("./#{dir}/factories/person_factory.rb") }
|
it { should require_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 require_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
|
||||||
it { should require_definitions_from("./#{dir}/factories/subdirectory/person_factory.rb") }
|
it { should require_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue