Skip to content

Commit

Permalink
Allow factory_girl_rails to auto configure factory girl as the fixtur…
Browse files Browse the repository at this point in the history
…e replacement mechanism.
  • Loading branch information
mikegehard committed Sep 24, 2011
1 parent c7d54d2 commit 97222f2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
factory_girl_rails (1.1.0)
factory_girl_rails (1.2.0)
factory_girl (~> 2.1.0)
railties (>= 3.0.0)

Expand Down
31 changes: 31 additions & 0 deletions features/fixture_replacement_config.feature
@@ -0,0 +1,31 @@
@disable-bundler
Feature:
In order to not have to manually configure factory girl as the testing fixture replacement
by using the --fixture-replacement=factory_girl option
as a Rails3 and Factory Girl user
I would like the Factory Girl Rails gem to configure Factory Girl
as the fixture replacement.

Background:
Given I successfully run `bundle exec rails new testapp`
And I cd to "testapp"
And I add "factory_girl_rails" from this project as a dependency

Scenario: Using Factory Girl and Factory Girl Rails with Test Unit generates
a factory file and does not generate a fixture file
And I successfully run `bundle install`
And I successfully run `bundle exec rails generate model User name:string`
Then the following files should exist:
| test/factories/users.rb |
And the following files should not exist:
| test/fixtures/users.yml |


Scenario: Using Factory Girl and Factory Girl Rails with RSpec should generate a factory file
And I add "rspec-rails" as a dependency
And I successfully run `bundle install`
And I successfully run `bundle exec rails generate model User name:string`
Then the following files should exist:
| spec/factories/users.rb |
And the following files should not exist:
| spec/fixtures/users.yml |
6 changes: 5 additions & 1 deletion features/step_definitions/rails_steps.rb
@@ -1,3 +1,7 @@
When /^I add "([^"]+)" from this project as a dependency$/ do |gem_name|
append_to_file('Gemfile', %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"})
append_to_file('Gemfile', %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"\n})
end

When /^I add "([^"]+)" as a dependency$/ do |gem_name|
append_to_file('Gemfile', %{gem "#{gem_name}"\n})
end
17 changes: 14 additions & 3 deletions lib/factory_girl_rails/railtie.rb
Expand Up @@ -3,11 +3,22 @@

module FactoryGirl
class Railtie < Rails::Railtie

initializer "factory_girl.set_fixture_replacement" do
generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators

if generators.options[:rails][:test_framework] == :rspec
generators.fixture_replacement :factory_girl, :dir => 'spec/factories'
else
generators.test_framework :test_unit, :fixture => false, :fixture_replacement => :factory_girl
end
end

config.after_initialize do
FactoryGirl.definition_file_paths = [
File.join(Rails.root, 'factories'),
File.join(Rails.root, 'test', 'factories'),
File.join(Rails.root, 'spec', 'factories')
File.join(Rails.root, 'factories'),
File.join(Rails.root, 'test', 'factories'),
File.join(Rails.root, 'spec', 'factories')
]
FactoryGirl.find_definitions
end
Expand Down

0 comments on commit 97222f2

Please sign in to comment.