├── Gemfile ├── lib ├── factory_girl_rails.rb ├── factory_girl_rails │ ├── generators │ │ ├── null_generator.rb │ │ ├── non_rspec_generator.rb │ │ └── rspec_generator.rb │ ├── railtie.rb │ └── generator.rb └── generators │ ├── factory_girl │ └── model │ │ ├── templates │ │ └── fixtures.erb │ │ └── model_generator.rb │ └── factory_girl.rb ├── .gitignore ├── features ├── step_definitions │ ├── appraisal.rb │ └── rails_steps.rb ├── support │ └── env.rb ├── generators.feature ├── load_definitions.feature └── fixture_replacement_config.feature ├── .travis.yml ├── NEWS ├── gemfiles ├── rails4.0.gemfile ├── rails3.2.gemfile └── rails3.1.gemfile ├── Rakefile ├── LICENSE ├── factory_girl_rails.gemspec ├── Appraisals ├── CONTRIBUTING.md └── README.md /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /lib/factory_girl_rails.rb: -------------------------------------------------------------------------------- 1 | require 'factory_girl_rails/railtie' 2 | 3 | module FactoryGirlRails 4 | end 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | test.db 3 | factory_girl_rails-*.gem 4 | tmp 5 | rdoc 6 | coverage 7 | pkg 8 | .bundle 9 | Gemfile.lock 10 | gemfiles/*.gemfile.lock 11 | -------------------------------------------------------------------------------- /lib/factory_girl_rails/generators/null_generator.rb: -------------------------------------------------------------------------------- 1 | module FactoryGirlRails 2 | module Generators 3 | class NullGenerator 4 | def initialize(generators) 5 | end 6 | 7 | def run 8 | end 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /features/step_definitions/appraisal.rb: -------------------------------------------------------------------------------- 1 | When /^I run `([^"]+)` with a clean environment$/ do |command| 2 | step %{I successfully run `ruby -e 'system({"BUNDLE_GEMFILE" => nil}, "#{command}")'`} 3 | end 4 | 5 | # system({'BUNDLE_GEMFILE' => nil}, "cd tmp/aruba/testapp && #{command} && cd -") 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | rvm: 2 | - 1.9.3 3 | - 2.0.0 4 | - jruby-19mode 5 | before_install: 6 | - gem update --system 7 | jdk: 8 | - openjdk6 9 | gemfile: 10 | - gemfiles/rails3.1.gemfile 11 | - gemfiles/rails3.2.gemfile 12 | - gemfiles/rails4.0.gemfile 13 | branches: 14 | only: 15 | - master 16 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | 4.2.1 (February 8, 2013) 2 | Fix bug when configuring FG and RSpec fixture directory 3 | Remove debugging 4 | Require factory_girl_rails explicitly in generator 5 | 6 | 4.2.0 (January 25, 2013) 7 | Add appraisal and get test suite working reliably with turn gem 8 | Support MiniTest 9 | Allow a custom directory for factories to be specified 10 | -------------------------------------------------------------------------------- /lib/generators/factory_girl/model/templates/fixtures.erb: -------------------------------------------------------------------------------- 1 | # Read about factories at https://github.com/thoughtbot/factory_girl 2 | 3 | FactoryGirl.define do 4 | factory :<%= singular_table_name %><%= explicit_class_option %> do 5 | <% for attribute in attributes -%> 6 | <%= attribute.name %> <%= attribute.default.inspect %> 7 | <% end -%> 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /gemfiles/rails4.0.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails" 6 | gem "sass-rails" 7 | gem "coffee-rails" 8 | gem "uglifier" 9 | gem "sqlite3", ">= 1.3.4", :platforms=>:mri 10 | gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.2", :platforms=>:jruby 11 | gem "jruby-openssl", :platforms=>:jruby 12 | gem "minitest-rails" 13 | gem "therubyrhino" 14 | gem "jquery-rails" 15 | gem "rspec-rails" 16 | 17 | gemspec :path=>"../" -------------------------------------------------------------------------------- /gemfiles/rails3.2.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "~> 3.2.15" 6 | gem "sass-rails" 7 | gem "coffee-rails" 8 | gem "uglifier" 9 | gem "sqlite3", ">= 1.3.4", :platforms=>:mri 10 | gem "activerecord-jdbcsqlite3-adapter", "~> 1.2.5", :platforms=>:jruby 11 | gem "jruby-openssl", :platforms=>:jruby 12 | gem "minitest-rails" 13 | gem "therubyrhino" 14 | gem "jquery-rails" 15 | gem "rspec-rails" 16 | 17 | gemspec :path=>"../" -------------------------------------------------------------------------------- /gemfiles/rails3.1.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "rails", "~> 3.1.12" 6 | gem "sass-rails", "~> 3.1.5" 7 | gem "coffee-rails" 8 | gem "uglifier" 9 | gem "sqlite3", ">= 1.3.4", :platforms=>:mri 10 | gem "activerecord-jdbcsqlite3-adapter", "~> 1.2.5", :platforms=>:jruby 11 | gem "jruby-openssl", :platforms=>:jruby 12 | gem "minitest-rails" 13 | gem "therubyrhino" 14 | gem "jquery-rails" 15 | gem "rspec-rails" 16 | gem "turn", "0.8.3" 17 | 18 | gemspec :path=>"../" -------------------------------------------------------------------------------- /lib/factory_girl_rails/generators/non_rspec_generator.rb: -------------------------------------------------------------------------------- 1 | module FactoryGirlRails 2 | module Generators 3 | class NonRSpecGenerator 4 | def initialize(generators) 5 | @generators = generators 6 | end 7 | 8 | def run 9 | @generators.test_framework test_framework, fixture: false, fixture_replacement: :factory_girl 10 | end 11 | 12 | private 13 | 14 | def test_framework 15 | @generators.options[:rails][:test_framework] 16 | end 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/generators/factory_girl.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators/named_base' 2 | 3 | module FactoryGirl 4 | module Generators 5 | class Base < Rails::Generators::NamedBase #:nodoc: 6 | def self.source_root 7 | @_factory_girl_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'factory_girl', generator_name, 'templates')) 8 | end 9 | 10 | def explicit_class_option 11 | ", :class => '#{class_name}'" unless class_name == singular_table_name.camelize 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/setup' 2 | require 'bundler/gem_tasks' 3 | require 'cucumber/rake/task' 4 | 5 | Cucumber::Rake::Task.new(:cucumber) do |t| 6 | t.fork = true 7 | t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')] 8 | end 9 | 10 | require 'appraisal' 11 | 12 | desc 'Run the test suite' 13 | task :default do |t| 14 | if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/ 15 | exec 'rake cucumber' 16 | else 17 | Rake::Task['appraise'].execute 18 | end 19 | end 20 | 21 | task :appraise => ['appraisal:install'] do |t| 22 | exec 'rake appraisal' 23 | end 24 | -------------------------------------------------------------------------------- /lib/factory_girl_rails/generators/rspec_generator.rb: -------------------------------------------------------------------------------- 1 | module FactoryGirlRails 2 | module Generators 3 | class RSpecGenerator 4 | def initialize(generators) 5 | @generators = generators 6 | end 7 | 8 | def run 9 | @generators.fixture_replacement fixture_replacement_setting, dir: factory_girl_directory 10 | end 11 | 12 | private 13 | 14 | def fixture_replacement_setting 15 | @generators.options[:rails][:fixture_replacement] || :factory_girl 16 | end 17 | 18 | def factory_girl_directory 19 | @generators.options[:factory_girl][:dir] || 'spec/factories' 20 | end 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/factory_girl_rails/railtie.rb: -------------------------------------------------------------------------------- 1 | require 'factory_girl' 2 | require 'factory_girl_rails/generator' 3 | require 'rails' 4 | 5 | module FactoryGirl 6 | class Railtie < Rails::Railtie 7 | 8 | initializer "factory_girl.set_fixture_replacement" do 9 | FactoryGirlRails::Generator.new(config).run 10 | end 11 | 12 | initializer "factory_girl.set_factory_paths" do 13 | FactoryGirl.definition_file_paths = [ 14 | Rails.root.join('factories'), 15 | Rails.root.join('test', 'factories'), 16 | Rails.root.join('spec', 'factories') 17 | ] 18 | end 19 | 20 | config.after_initialize do 21 | FactoryGirl.find_definitions 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /features/support/env.rb: -------------------------------------------------------------------------------- 1 | require 'aruba/cucumber' 2 | 3 | PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')).freeze 4 | 5 | Before do 6 | @aruba_timeout_seconds = 3600 7 | end 8 | 9 | Aruba.configure do |config| 10 | config.before_cmd do |cmd| 11 | set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived 12 | # force jRuby to use client JVM for faster startup times 13 | if 1.size == 4 ##equals 4 for 32 bit java, 8 for 64 bit java. 14 | set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") 15 | else 16 | set_env('JAVA_OPTS', "-XX:+TieredCompilation -XX:TieredStopAtLevel=1 #{ENV['JAVA_OPTS']}") 17 | end 18 | end 19 | end if RUBY_PLATFORM == 'java' 20 | -------------------------------------------------------------------------------- /lib/generators/factory_girl/model/model_generator.rb: -------------------------------------------------------------------------------- 1 | require 'generators/factory_girl' 2 | require 'factory_girl_rails' 3 | 4 | module FactoryGirl 5 | module Generators 6 | class ModelGenerator < Base 7 | argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" 8 | class_option :dir, :type => :string, :default => "test/factories", :desc => "The directory where the factories should go" 9 | 10 | def create_fixture_file 11 | filename = [table_name, filename_suffix].compact.join('_') 12 | template 'fixtures.erb', File.join(options[:dir], "#{filename}.rb") 13 | end 14 | 15 | private 16 | 17 | def filename_suffix 18 | factory_girl_options[:suffix] 19 | end 20 | 21 | def generators 22 | config = FactoryGirl::Railtie.config 23 | config.respond_to?(:app_generators) ? config.app_generators : config.generators 24 | end 25 | 26 | def factory_girl_options 27 | generators.options[:factory_girl] || {} 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008-2013 Joe Ferris and thoughtbot, inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /lib/factory_girl_rails/generator.rb: -------------------------------------------------------------------------------- 1 | require 'factory_girl_rails/generators/rspec_generator' 2 | require 'factory_girl_rails/generators/non_rspec_generator' 3 | require 'factory_girl_rails/generators/null_generator' 4 | 5 | module FactoryGirlRails 6 | class Generator 7 | def initialize(config) 8 | @generators = if config.respond_to?(:app_generators) 9 | config.app_generators 10 | else 11 | config.generators 12 | end 13 | end 14 | 15 | def run 16 | generator.new(@generators).run 17 | end 18 | 19 | def generator 20 | if factory_girl_disabled? 21 | Generators::NullGenerator 22 | else 23 | if test_framework == :rspec 24 | Generators::RSpecGenerator 25 | else 26 | Generators::NonRSpecGenerator 27 | end 28 | end 29 | end 30 | 31 | def test_framework 32 | rails_options[:test_framework] 33 | end 34 | 35 | def factory_girl_disabled? 36 | rails_options[:factory_girl] == false 37 | end 38 | 39 | def rails_options 40 | @generators.options[:rails] 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /factory_girl_rails.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = %q{factory_girl_rails} 3 | s.version = '4.3.0' 4 | s.authors = ["Joe Ferris"] 5 | s.email = %q{jferris@thoughtbot.com} 6 | s.homepage = "http://github.com/thoughtbot/factory_girl_rails" 7 | s.summary = %q{factory_girl_rails provides integration between 8 | factory_girl and rails 3} 9 | s.description = %q{factory_girl_rails provides integration between 10 | factory_girl and rails 3 (currently just automatic factory definition 11 | loading)} 12 | 13 | s.files = `git ls-files`.split("\n") 14 | s.test_files = `git ls-files -- Appraisals {spec,features,gemfiles}/*`.split("\n") 15 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 16 | s.require_paths = ["lib"] 17 | s.license = "MIT" 18 | 19 | s.add_runtime_dependency('railties', '>= 3.0.0') 20 | s.add_runtime_dependency('factory_girl', '~> 4.3.0') 21 | 22 | s.add_development_dependency('appraisal', '~> 0.5.0') 23 | s.add_development_dependency('rake') 24 | s.add_development_dependency('rspec', '~> 2.11.0') 25 | s.add_development_dependency('cucumber', '~> 1.2.1') 26 | s.add_development_dependency('aruba', '~> 0.5.1') 27 | end 28 | -------------------------------------------------------------------------------- /Appraisals: -------------------------------------------------------------------------------- 1 | appraise "rails3.1" do 2 | gem "rails", "~> 3.1.12" 3 | gem "sass-rails", "~> 3.1.5" 4 | gem "coffee-rails" 5 | gem "uglifier" 6 | gem "sqlite3", ">= 1.3.4", platforms: :mri 7 | gem "activerecord-jdbcsqlite3-adapter", "~> 1.2.5", platforms: :jruby 8 | gem "jruby-openssl", platforms: :jruby 9 | gem "minitest-rails" 10 | gem "therubyrhino" 11 | gem "jquery-rails" 12 | gem "rspec-rails" 13 | gem "turn", "0.8.3" 14 | end 15 | 16 | appraise "rails3.2" do 17 | gem "rails", "~> 3.2.15" 18 | gem "sass-rails" 19 | gem "coffee-rails" 20 | gem "uglifier" 21 | gem "sqlite3", ">= 1.3.4", platforms: :mri 22 | gem "activerecord-jdbcsqlite3-adapter", "~> 1.2.5", platforms: :jruby 23 | gem "jruby-openssl", platforms: :jruby 24 | gem "minitest-rails" 25 | gem "therubyrhino" 26 | gem "jquery-rails" 27 | gem "rspec-rails" 28 | end 29 | 30 | appraise "rails4.0" do 31 | gem "rails" 32 | gem "sass-rails" 33 | gem "coffee-rails" 34 | gem "uglifier" 35 | gem "sqlite3", ">= 1.3.4", platforms: :mri 36 | gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.2", platforms: :jruby 37 | gem "jruby-openssl", platforms: :jruby 38 | gem "minitest-rails" 39 | gem "therubyrhino" 40 | gem "jquery-rails" 41 | gem "rspec-rails" 42 | end 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | We love pull requests. Here's a quick guide: 2 | 3 | 1. Fork the repo. 4 | 5 | 2. Run the tests. We only take pull requests with passing tests, and it's great 6 | to know that you have a clean slate: `bundle && bundle exec rake appraisal:install && bundle exec rake appraisal` 7 | 8 | 3. Add a test for your change. Only refactoring and documentation changes 9 | require no new tests. If you are adding functionality or fixing a bug, we need 10 | a test! 11 | 12 | 4. Make the test pass. 13 | 14 | 5. Push to your fork and submit a pull request. 15 | 16 | 17 | At this point you're waiting on us. We like to at least comment on, if not 18 | accept, pull requests within three business days (and, typically, one business 19 | day). We may suggest some changes or improvements or alternatives. 20 | 21 | Some things that will increase the chance that your pull request is accepted, 22 | taken straight from the Ruby on Rails guide: 23 | 24 | * Use Rails idioms and helpers 25 | * Include tests that fail without your code, and pass with it 26 | * Update the documentation, the surrounding one, examples elsewhere, guides, 27 | whatever is affected by your contribution 28 | 29 | Syntax: 30 | 31 | * Two spaces, no tabs. 32 | * No trailing whitespace. Blank lines should not have any space. 33 | * Prefer &&/|| over and/or. 34 | * MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg. 35 | * a = b and not a=b. 36 | * Follow the conventions you see used in the source already. 37 | 38 | And in case we didn't emphasize it enough: we love tests! 39 | -------------------------------------------------------------------------------- /features/step_definitions/rails_steps.rb: -------------------------------------------------------------------------------- 1 | When /^I add "([^"]+)" from this project as a dependency$/ do |gem_name| 2 | append_to_file('Gemfile', %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"\n}) 3 | end 4 | 5 | When /^I add "([^"]+)" as a dependency$/ do |gem_name| 6 | append_to_file('Gemfile', %{gem "#{gem_name}"\n}) 7 | end 8 | 9 | When /^I set the FactoryGirl :suffix option to "([^"]+)"$/ do |suffix| 10 | append_to_file('config/application.rb', <<-RUBY) 11 | module Testapp 12 | class Application < Rails::Application 13 | config.generators do |g| 14 | g.fixture_replacement :factory_girl, :suffix => '#{suffix}' 15 | end 16 | end 17 | end 18 | RUBY 19 | 20 | end 21 | 22 | When /^I configure the factories as:$/ do |string| 23 | append_to_file File.join('config', 'application.rb'), <<-END 24 | class Testapp::Application 25 | #{string} 26 | end 27 | END 28 | end 29 | 30 | When /^I configure the factories directory as "([^"]+)"$/ do |factory_dir| 31 | append_to_file File.join('config', 'application.rb'), <<-END 32 | class Testapp::Application 33 | config.generators do |g| 34 | g.fixture_replacement :factory_girl, :dir => "#{factory_dir}" 35 | end 36 | end 37 | END 38 | end 39 | 40 | When /^I configure the testing framework to use MiniTest$/ do 41 | append_to_file('Gemfile', %{gem "minitest-rails", :group => [:development, :test]\n}) 42 | step %{I run `bundle install` with a clean environment} 43 | step %{I run `rails generate mini_test:install -f` with a clean environment} 44 | 45 | append_to_file File.join('config', 'application.rb'), <<-END 46 | class Testapp::Application 47 | config.generators do |g| 48 | g.test_framework :mini_test, :fixture => false, :fixture_replacement => :factory_girl 49 | end 50 | end 51 | END 52 | end 53 | 54 | When /^I comment out gem "([^"]*)" from my Gemfile$/ do |gem_name| 55 | in_current_dir do 56 | content = File.read('Gemfile') 57 | File.open('Gemfile', 'w') do |f| 58 | f.write content.sub(/gem ['"]#{gem_name}/, '#\1') 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | factory_girl [![Build Status](https://secure.travis-ci.org/thoughtbot/factory_girl_rails.png)](http://travis-ci.org/thoughtbot/factory_girl_rails?branch=master) 2 | ============ 3 | 4 | `factory_girl` is a fixtures replacement with a straightforward definition 5 | syntax, support for multiple build strategies (saved instances, unsaved 6 | instances, attribute hashes, and stubbed objects), and support for multiple 7 | factories for the same class (`user`, `admin_user`, and so on), including factory 8 | inheritance. 9 | 10 | Rails 11 | ----- 12 | 13 | `factory_girl_rails` provides Rails integration for `factory_girl`. All 14 | Rails-specific features are only compatible with Rails 3. 15 | 16 | Currently, automatic factory definition loading is the only Rails-specific feature. 17 | 18 | Download 19 | -------- 20 | 21 | Github: http://github.com/thoughtbot/factory_girl_rails/tree/master 22 | 23 | Gem: 24 | 25 | gem install factory_girl_rails 26 | 27 | Configuration 28 | ------------- 29 | 30 | Add `factory_girl_rails` to your Gemfile: 31 | 32 | gem 'factory_girl_rails' 33 | 34 | Generators for factories will automatically substitute fixture (and maybe any other 35 | `fixture_replacement` you set). If you want disable this feature, add the 36 | following to your application.rb file: 37 | 38 | config.generators do |g| 39 | g.factory_girl false 40 | end 41 | 42 | Default factories directory is `test/factories`, or `spec/factories` if 43 | `test_framework` generator is set to `:rspec`; change this behavior with: 44 | 45 | config.generators do |g| 46 | g.factory_girl dir: 'custom/dir/for/factories' 47 | end 48 | 49 | If you use `factory_girl` for fixture replacement, ensure that 50 | `factory_girl_rails` is available in the development group. If it's not, Rails 51 | will generate standard yml files instead of factory files. 52 | 53 | `factory_girl` takes an option `suffix: 'some_suffix'` 54 | to generate factories as "modelname_some_suffix.rb" 55 | 56 | More Information 57 | ---------------- 58 | 59 | factory_girl: http://github.com/thoughtbot/factory_girl/tree/master 60 | 61 | 62 | Contributing 63 | ------------ 64 | 65 | Please see CONTRIBUTING.md for details. 66 | 67 | Credits 68 | ------- 69 | 70 | factory_girl was originally written by Joe Ferris. 71 | 72 | ![thoughtbot](http://thoughtbot.com/images/tm/logo.png) 73 | 74 | factory_girl is maintained and funded by [thoughtbot, inc](http://thoughtbot.com/community) 75 | 76 | The names and logos for thoughtbot are trademarks of thoughtbot, inc. 77 | 78 | License 79 | ------- 80 | 81 | factory_girl is Copyright © 2008-2013 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file. 82 | -------------------------------------------------------------------------------- /features/generators.feature: -------------------------------------------------------------------------------- 1 | Feature: 2 | In order to easily generate factory files instead of fixture files when generating models 3 | As a user of Rails3 and factory_girl 4 | I would like to use factory_girl_rails generators. 5 | 6 | Background: 7 | Given I successfully run `bundle exec rails new testapp` 8 | And I cd to "testapp" 9 | And I add "factory_girl_rails" from this project as a dependency 10 | 11 | Scenario: The factory_girl_rails generators create a factory file for each model that I generate 12 | When I run `bundle install` with a clean environment 13 | And I run `bundle exec rails generate model User name:string --fixture-replacement=factory_girl` with a clean environment 14 | And I run `bundle exec rails generate model Namespaced::User name:string --fixture-replacement=factory_girl` with a clean environment 15 | Then the output should contain "test/factories/users.rb" 16 | And the output should contain "test/factories/namespaced_users.rb" 17 | And the file "test/factories/users.rb" should contain "factory :user do" 18 | And the file "test/factories/namespaced_users.rb" should contain "factory :namespaced_user, :class => 'Namespaced::User' do" 19 | 20 | Scenario: The factory_girl_rails generators create a factory file with a custom name for each model that I generate 21 | When I run `bundle install` with a clean environment 22 | And I set the FactoryGirl :suffix option to "factory" 23 | And I run `bundle exec rails generate model User name:string --fixture-replacement=factory_girl` with a clean environment 24 | And I run `bundle exec rails generate model Namespaced::User name:string --fixture-replacement=factory_girl` with a clean environment 25 | Then the output should contain "test/factories/users_factory.rb" 26 | And the output should contain "test/factories/namespaced_users_factory.rb" 27 | And the file "test/factories/users_factory.rb" should contain "factory :user do" 28 | And the file "test/factories/namespaced_users_factory.rb" should contain "factory :namespaced_user, :class => 'Namespaced::User' do" 29 | 30 | Scenario: The factory_girl_rails generators create a factory file using configured suffix with RSpec for each model that I generate 31 | When I add "rspec-rails" as a dependency 32 | And I configure the factories as: 33 | """ 34 | config.generators do |g| 35 | g.test_framework :rspec 36 | g.fixture_replacement :factory_girl, suffix: 'factory' 37 | end 38 | """ 39 | And I run `bundle install` with a clean environment 40 | Then the output should contain "rspec-rails" 41 | And I run `bundle exec rails generate model User name:string` with a clean environment 42 | Then the following files should exist: 43 | | spec/factories/users_factory.rb | 44 | -------------------------------------------------------------------------------- /features/load_definitions.feature: -------------------------------------------------------------------------------- 1 | Feature: automatically load step definitions 2 | 3 | Background: 4 | When I successfully run `bundle exec rails new testapp` 5 | And I cd to "testapp" 6 | And I add "factory_girl_rails" from this project as a dependency 7 | And I comment out gem "turn" from my Gemfile 8 | And I run `bundle install` with a clean environment 9 | And I write to "db/migrate/1_create_users.rb" with: 10 | """ 11 | class CreateUsers < ActiveRecord::Migration 12 | def self.up 13 | create_table :users do |t| 14 | t.string :name 15 | end 16 | end 17 | end 18 | """ 19 | When I run `bundle exec rake db:migrate --trace` with a clean environment 20 | And I write to "app/models/user.rb" with: 21 | """ 22 | class User < ActiveRecord::Base 23 | end 24 | """ 25 | 26 | Scenario: generate a rails 3 application and use factory definitions 27 | When I write to "test/factories.rb" with: 28 | """ 29 | FactoryGirl.define do 30 | factory :user do 31 | name "Frank" 32 | end 33 | end 34 | """ 35 | When I write to "test/unit/user_test.rb" with: 36 | """ 37 | require 'test_helper' 38 | 39 | class UserTest < ActiveSupport::TestCase 40 | test "use factory" do 41 | user = FactoryGirl.create(:user) 42 | assert_equal 'Frank', user.name 43 | end 44 | end 45 | """ 46 | When I run `bundle exec rake test --trace` with a clean environment 47 | Then the output should contain "1 tests, 1 assertions, 0 failures, 0 errors" 48 | 49 | Scenario: use factories advertised by railties/engines/3rd-party gems 50 | When I append to "config/application.rb" with: 51 | """ 52 | require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) 53 | """ 54 | When I write to "lib/some_railtie/railties.rb" with: 55 | """ 56 | module SomeRailtie 57 | class Railtie < ::Rails::Engine 58 | 59 | initializer "some_railtie.factories", :after => "factory_girl.set_factory_paths" do 60 | FactoryGirl.definition_file_paths << File.expand_path('../factories', __FILE__) 61 | end 62 | end 63 | end 64 | """ 65 | When I write to "lib/some_railtie/factories.rb" with: 66 | """ 67 | FactoryGirl.define do 68 | factory :factory_from_some_railtie, :class => 'User' do 69 | name 'Artem' 70 | end 71 | end 72 | """ 73 | When I write to "test/unit/user_test.rb" with: 74 | """ 75 | require 'test_helper' 76 | 77 | class UserTest < ActiveSupport::TestCase 78 | test "use factory of some_railtie" do 79 | user = FactoryGirl.create(:factory_from_some_railtie) 80 | assert_equal 'Artem', user.name 81 | end 82 | end 83 | """ 84 | When I run `bundle exec rake test --trace` with a clean environment 85 | Then the output should contain "1 tests, 1 assertions, 0 failures, 0 errors" 86 | -------------------------------------------------------------------------------- /features/fixture_replacement_config.feature: -------------------------------------------------------------------------------- 1 | Feature: 2 | 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 3 | I would like the Factory Girl Rails gem to configure Factory Girl as the fixture replacement. 4 | 5 | Background: 6 | Given I successfully run `bundle exec rails new testapp` 7 | And I cd to "testapp" 8 | And I add "factory_girl_rails" from this project as a dependency 9 | 10 | Scenario: Using Factory Girl and Factory Girl Rails with Test Unit generates a factory file and does not generate a fixture file 11 | And I run `bundle install` with a clean environment 12 | And I run `bundle exec rails generate model User name:string` with a clean environment 13 | Then the following files should exist: 14 | | test/factories/users.rb | 15 | And the following files should not exist: 16 | | test/fixtures/users.yml | 17 | 18 | Scenario: Using Factory Girl and Factory Girl Rails with RSpec should generate a factory file 19 | When I add "rspec-rails" as a dependency 20 | And I configure the factories as: 21 | """ 22 | config.generators do |g| 23 | g.test_framework :rspec, fixture: true 24 | g.fixture_replacement :factory_girl 25 | end 26 | """ 27 | And I run `bundle install` with a clean environment 28 | Then the output should contain "rspec-rails" 29 | And I run `bundle exec rails generate model User name:string` with a clean environment 30 | Then the following files should exist: 31 | | spec/factories/users.rb | 32 | And the following files should not exist: 33 | | spec/fixtures/users.yml | 34 | 35 | Scenario: Using Factory Girl and Factory Girl Rails does not override a manually-configured factories directory using RSpec 36 | When I add "rspec-rails" as a dependency 37 | And I configure the factories directory as "custom/dir" 38 | And I run `bundle install` with a clean environment 39 | Then the output should contain "rspec-rails" 40 | And I run `bundle exec rails generate model User name:string` with a clean environment 41 | Then the following files should not exist: 42 | | test/factories/users.rb | 43 | | spec/factories/users.rb | 44 | But the following files should exist: 45 | | custom/dir/users.rb | 46 | 47 | Scenario: Using Factory Girl and Factory Girl Rails does not override a manually-configured factories directory using Test::Unit 48 | When I configure the factories directory as "custom/dir" 49 | And I run `bundle install` with a clean environment 50 | And I run `bundle exec rails generate model User name:string` with a clean environment 51 | Then the following files should not exist: 52 | | test/factories/users.rb | 53 | | spec/factories/users.rb | 54 | But the following files should exist: 55 | | custom/dir/users.rb | 56 | 57 | Scenario: Using Factory Girl and Factory Girl Rails with MiniTest should generate a factory file 58 | When I configure the testing framework to use MiniTest 59 | And I run `bundle exec rails generate model User name:string` with a clean environment 60 | Then the following files should exist: 61 | | test/factories/users.rb | 62 | But the following files should not exist: 63 | | spec/fixtures/users.yml | 64 | 65 | Scenario: Using Factory Girl and Factory Girl Rails with MiniTest and a custom directory should generate a factory file 66 | When I configure the factories directory as "custom/dir" 67 | And I configure the testing framework to use MiniTest 68 | And I run `bundle exec rails generate model User name:string` with a clean environment 69 | Then the following files should exist: 70 | | custom/dir/users.rb | 71 | But the following files should not exist: 72 | | spec/fixtures/users.yml | 73 | 74 | Scenario: Disable Factory Girl generator 75 | When I configure the factories as: 76 | """ 77 | config.generators do |g| 78 | g.factory_girl false 79 | end 80 | """ 81 | And I run `bundle install` with a clean environment 82 | And I run `bundle exec rails generate model User name:string` with a clean environment 83 | Then the following files should not exist: 84 | | test/factories/users.rb | 85 | | spec/factories/users.rb | 86 | --------------------------------------------------------------------------------