Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow factory_girl_rails to auto configure factory girl as the fixtur…

…e replacement mechanism.
  • Loading branch information...
commit 97222f2b58531c6f0d38de17496a54e589143f7c 1 parent c7d54d2
@mikegehard mikegehard authored
View
2  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)
View
31 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 |
View
6 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
View
17 lib/factory_girl_rails/railtie.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.