Skip to content
Browse files

first bid at a factory girl generator. appends the factory definition…

… to end of test/factories.rb by default. definitions file can be overriden in config/application.rb
  • Loading branch information...
1 parent 338105c commit 5c0b862dacf23e35fbaebb3d04f68370bcaa6009 @croaky croaky committed Jul 6, 2010
View
42 features/generator.feature
@@ -0,0 +1,42 @@
+Feature: Use Factory Girl instead of Fixtures for generators
+
+ Scenario: generate a rails 3 application and use factory definitions
+ When I generate a new rails application
+ And I save the following as "Gemfile"
+ """
+ source "http://rubygems.org"
+ gem 'rails', '3.0.0.beta4'
+ gem 'sqlite3-ruby', :require => 'sqlite3'
+ gem 'factory_girl_rails', :path => '../../'
+ """
+ When I run "bundle lock"
+ And I save the following as "config/application.rb"
+ """
+ require File.expand_path('../boot', __FILE__)
+ require 'rails/all'
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
+ module Testapp
+ class Application < Rails::Application
+ config.generators do |g|
+ g.test_framework :test_unit, :fixture => false
+ g.fixture_replacement :factory_girl
+ end
+ end
+ end
+ """
+ When I run "rails g model User name:string"
+ And I run "rake db:migrate"
+ And I save the following as "test/unit/user_test.rb"
+ """
+ require 'test_helper'
+
+ class UserTest < ActiveSupport::TestCase
+ test "use factory" do
+ user = Factory(:user)
+ assert_equal 'MyString', user.name
+ end
+ end
+ """
+ When I run "rails g factory_girl User name:string"
+ And I run "rake test"
+ Then I should see "1 tests, 1 assertions, 0 failures, 0 errors"
View
1 features/step_definitions/rails_steps.rb
@@ -29,4 +29,3 @@
raise("Got terminal output:\n#{@terminal.output}\n\nExpected output:\n#{expected_text}")
end
end
-
View
14 lib/generators/factory_girl_generator.rb
@@ -0,0 +1,14 @@
+class FactoryGirlGenerator < Rails::Generators::NamedBase
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+ class_option :file, :type => :string, :default => "test/factories.rb", :desc => "The file where factory definitions go."
+
+ def create_fixture_file
+ define_line = "Factory.define :#{singular_name} do |factory|"
+ attributes_lines = attributes.map {|a| " factory.#{a.name} { #{a.default.inspect} }" }.join('\n')
+
+ create_file options[:file]
+ append_file options[:file], "#{define_line}\n#{attributes_lines}\nend"
+ end
+
+ hook_for :fixture_replacement
+end

0 comments on commit 5c0b862

Please sign in to comment.
Something went wrong with that request. Please try again.