Coder Social home page Coder Social logo

factory_bot_rails's Introduction

factory_bot_rails Code Climate Gem Version

factory_bot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.

Transitioning from factory_girl_rails?

Check out the guide.

Rails

factory_bot_rails provides Rails integration for factory_bot.

Supported Rails versions are listed in Appraisals. Supported Ruby versions are listed in .github/workflows/build.yml.

Download

Github: http://github.com/thoughtbot/factory_bot_rails

Gem:

$ gem install factory_bot_rails

Configuration

Add factory_bot_rails to your Gemfile in both the test and development groups:

group :development, :test do
  gem 'factory_bot_rails'
end

You may want to configure your test suite to include factory_bot methods; see configuration.

Automatic Factory Definition Loading

By default, factory_bot_rails will automatically load factories defined in the following locations, relative to the root of the Rails project:

factories.rb
test/factories.rb
spec/factories.rb
factories/*.rb
test/factories/*.rb
spec/factories/*.rb

You can configure by adding the following to config/application.rb or the appropriate environment configuration in config/environments:

config.factory_bot.definition_file_paths = ["custom/factories"]

This will cause factory_bot_rails to automatically load factories in custom/factories.rb and custom/factories/*.rb.

It is possible to use this setting to share factories from a gem:

begin
  require 'factory_bot_rails'
rescue LoadError
end

class MyEngine < ::Rails::Engine
  config.factory_bot.definition_file_paths +=
    [File.expand_path('../factories', __FILE__)] if defined?(FactoryBotRails)
end

You can also disable automatic factory definition loading entirely by using an empty array:

config.factory_bot.definition_file_paths = []

Generators

Including factory_bot_rails in the development group of your Gemfile will cause Rails to generate factories instead of fixtures. If you want to disable this feature, you can either move factory_bot_rails out of the development group of your Gemfile, or add the following configuration:

config.generators do |g|
  g.factory_bot false
end

If fixture replacement is enabled and you already have a test/factories.rb file (or spec/factories.rb if using rspec_rails), generated factories will be inserted at the top of the existing file. Otherwise, factories will be generated in the test/factories directory (spec/factories if using rspec_rails), in a file matching the name of the table (e.g. test/factories/users.rb).

To generate factories in a different directory, you can use the following configuration:

config.generators do |g|
  g.factory_bot dir: 'custom/dir/for/factories'
end

Note that factory_bot_rails will not automatically load files in custom locations unless you add them to config.factory_bot.definition_file_paths as well.

The suffix option allows you to customize the name of the generated file with a suffix:

config.generators do |g|
  g.factory_bot suffix: "factory"
end

This will generate test/factories/users_factory.rb instead of test/factories/users.rb.

For even more customization, use the filename_proc option:

config.generators do |g|
  g.factory_bot filename_proc: ->(table_name) { "prefix_#{table_name}_suffix" }
end

To override the default factory template, define your own template in lib/templates/factory_bot/model/factories.erb. This template will have access to any methods available in FactoryBot::Generators::ModelGenerator. Note that factory_bot_rails will only use this custom template if you are generating each factory in a separate file; it will have no effect if you are generating all of your factories in test/factories.rb or spec/factories.rb.

Factory_bot_rails will add a custom generator:

rails generate factory_bot:model NAME [field:type field:type] [options]

Contributing

Please see CONTRIBUTING.md.

factory_bot_rails was originally written by Joe Ferris and is maintained by thoughtbot. Many improvements and bugfixes were contributed by the open source community.

License

factory_bot_rails is Copyright © 2008 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About thoughtbot

thoughtbot

This repo is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects. We are available for hire.

factory_bot_rails's People

Contributors

alextrueman avatar amatsuda avatar atul9 avatar betelgeuse avatar budu avatar composerinteralia avatar cryo28 avatar dependabot[bot] avatar edudepetris avatar jaredbeck avatar jessieay avatar jferris avatar joshuaclayton avatar mike-burns avatar mikegehard avatar mjankowski avatar neilvcarvalho avatar odlp avatar okuramasafumi avatar olleolleolle avatar petedmarsh avatar petestreet avatar salbertson avatar seanpdoyle avatar spalenza avatar stefannibrasil avatar vsppedro avatar y-yagi avatar yahonda avatar ydah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

factory_bot_rails's Issues

1.5 gemspec is wrong

looks like something's wrong with the 1.5 gemspec.

Invalid gemspec in [/Users/john/.rbenv/versions/ree-1.8.7-2011.03/lib/ruby/gems/1.8/specifications/factory_girl_rails-1.5.0.gemspec]: Illformed requirement ["# 3.0.7"]

Breaks routing with Factory.stub under Rails 3.0.2

Under Rails 3.0.1, we used @f = Factory.stub(:factory_name). We then had a controller call redirect_to @f. This returned the correct routing to the resource for show action. However, the same code under Rails 3.0.2 fails. It appears that Factory.stub is missing some required elements for Rails under 3.0.2.

Uninitialized constant error with Rails 3.0.1

I can't get Factory Girl to work at all with a brand-new Rails 3.0.1 project. At first I was using the factory_girl gem and running into problems until I saw the note to use factory_girl_rails instead. I installed factory_girl_rails and put it in my gemfile, but I'm still having similar problems.

I created a bunch of factories in test/factories.rb, but attempting to load my test environment results in the following error:

/Users/Default/Sites/test.com/test/factories.rb:3: uninitialized constant FactoryGirl (NameError)
from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:307:in `find_definitions'
from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `each'
from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `find_definitions'
from /Library/Ruby/Gems/1.8/gems/factory_girl_rails-1.0/lib/factory_girl_rails/railtie.rb:11
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `call'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application/finisher.rb:46
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `instance_exec'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `run'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:50:in `run_initializers'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `each'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `run_initializers'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:134:in `initialize!'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `send'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `method_missing'
from /Users/Default/Sites/test.com/config/environment.rb:5
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:103:in `require'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:103:in `require_environment!'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/commands.rb:22
from script/rails:6:in `require'
from script/rails:6

Nothing I do with either the factory_girl or factory_girl_rails gem seems to get around the an uninitialized constant error with the FactoryGirl class. Is this a problem with the gem or am I doing something wrong?

factory_girl_rails breaks ruby-debug functionality when included in development environment

Hey guys,

the title says it all. Steps to reproduce:

  • add "gem 'factory_girl_rails'" to your gemfile (in development environment)
  • add "debugger" to an arbitrary controller action
  • start up a server via: "script/rails s --debugger"
  • call the controller action from before

You'll see the following error:
NoMethodError (private method `puts' called for nil:NilClass):

Full stacktrace:


NoMethodError (private method 'puts' called for nil:NilClass):
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/whiny_nil.rb:48:in 'method_missing'
  my_home/.bundle/ruby/1.8/gems/rspec-core-2.0.0.beta.16/lib/rspec/core/kernel_extensions.rb:3:in 'debugger'
  my_project/app/controllers/platform/entries_controller.rb:46:in 'update'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/implicit_render.rb:4:in 'send_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/implicit_render.rb:4:in 'send_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/abstract_controller/base.rb:145:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/rendering.rb:11:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/abstract_controller/callbacks.rb:18:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:468:in '_run__869068243__process_action__944252406__callbacks'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:408:in 'send'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:408:in '_run_process_action_callbacks'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:88:in 'send'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:88:in 'run_callbacks'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/abstract_controller/callbacks.rb:17:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/instrumentation.rb:29:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/notifications/instrumenter.rb:20:in 'instrument'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/notifications.rb:48:in '__send__'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/notifications.rb:48:in 'instrument'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/instrumentation.rb:28:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/rescue.rb:8:in 'process_action'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/abstract_controller/base.rb:114:in 'process'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/abstract_controller/rendering.rb:40:in 'process'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal.rb:126:in 'dispatch'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal/rack_delegation.rb:14:in 'dispatch'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_controller/metal.rb:166
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/routing/route_set.rb:27:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/routing/route_set.rb:27:in 'call'
  my_home/.bundle/ruby/1.8/gems/rack-mount-0.6.13/lib/rack/mount/route_set.rb:148:in 'call'
  my_home/.bundle/ruby/1.8/gems/rack-mount-0.6.13/lib/rack/mount/code_generation.rb:93:in 'recognize'
  my_home/.bundle/ruby/1.8/gems/rack-mount-0.6.13/lib/rack/mount/code_generation.rb:96:in 'optimized_each'
  my_home/.bundle/ruby/1.8/gems/rack-mount-0.6.13/lib/rack/mount/code_generation.rb:92:in 'recognize'
  my_home/.bundle/ruby/1.8/gems/rack-mount-0.6.13/lib/rack/mount/route_set.rb:139:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/routing/route_set.rb:457:in 'call'
  my_home/.bundle/ruby/1.8/gems/hoptoad_notifier-2.2.6/lib/hoptoad_notifier/rack.rb:27:in 'call'
  my_home/.bundle/ruby/1.8/gems/haml-3.0.10/lib/sass/plugin/rack.rb:41:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/head.rb:14:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/methodoverride.rb:24:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/params_parser.rb:21:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/flash.rb:177:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/session/abstract_store.rb:106:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/cookies.rb:235:in 'call'
  /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/query_cache.rb:31:in 'call'
  /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/connection_adapters/abstract/query_cache.rb:28:in 'cache'
  /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/query_cache.rb:11:in 'cache'
  /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/query_cache.rb:30:in 'call'
  /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/connection_adapters/abstract/connection_pool.rb:365:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/callbacks.rb:46:in 'call'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:414:in '_run_call_callbacks'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:88:in 'send'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:88:in 'run_callbacks'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/callbacks.rb:44:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/sendfile.rb:105:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/remote_ip.rb:48:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/show_exceptions.rb:48:in 'call'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/rack/logger.rb:14:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/runtime.rb:17:in 'call'
  /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/cache/strategy/local_cache.rb:72:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in 'synchronize'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in 'call'
  /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/static.rb:30:in 'call'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/application.rb:145:in 'call'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/application.rb:81:in 'send'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/application.rb:81:in 'method_missing'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/rack/debugger.rb:21:in 'call'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/rack/log_tailer.rb:15:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/content_length.rb:13:in 'call'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/handler/webrick.rb:48:in 'service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in 'service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in 'run'
  /usr/lib/ruby/1.8/webrick/server.rb:173:in 'start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in 'start'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in 'start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:95:in 'start'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in 'each'
  /usr/lib/ruby/1.8/webrick/server.rb:92:'in 'start'
  /usr/lib/ruby/1.8/webrick/server.rb:23:in 'start'
  /usr/lib/ruby/1.8/webrick/server.rb:82:in 'start'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/handler/webrick.rb:14:in 'run'
  /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/server.rb:155:in 'start'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands/server.rb:62:in 'start'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:30
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:27:in 'tap'
  /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:27
  script/rails:6:in 'require'
  script/rails:6

When I remove factory_girl_rails from the Gemfile the debugger works again.

ruby: ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
rails: rails-3.0.0.beta4
factory_girl_ralls: factory_girl_rails-1.0

unit tests being generated even with config.test_framework false

Steps to reproduce:

Rails 3.1.3 , Ruby 1.9.3-p0

rails new app
cd app

in Gemfile:

group :development, :test do
  gem 'factory_girl_rails'
end

in development.rb:

config.generators do |c|
  c.test_framework false
end
bundle
rails generate model foo bar:string

Unit tests are generated anyway.

Without the factory_girl_rails gem, the correct behaviour is observed (no tests generated).

Polymorphic Association in Rails 3.1.rc1

Tested with factory_girl_rails: 1.0.1 AND 1.1.beta1 .

Given 3 models:

class Picture < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
end

class Employee < ActiveRecord::Base
  has_one :picture, :as => :imageable
end

class Product < ActiveRecord::Base
  has_one :picture, :as => :imageable
end

AND given the following factories:

Factory.define(:picture) do |f|
  f.name = "my_picture"
end 

Factory.define(:employee) do |f|
  f.association :picture
end

Factory.define(:product) do |f|
  f.association :picture
end

In Rails3.0.5 UP TO Rails3.0.8.rc2:

e = Factory(:employee) 
e.picture.imageable_type
=> "Employee"

However, in Rails3.1.0.rc1:

e = Factory(:employee) 
e.picture.imageable_type
=> nil

I assume this is not intended -- but perhaps I am wrong or missing something...

Railtie definition_file_paths doesn't match core gem definition_file_paths

The factory_girl gem uses a definition_file_paths of

  self.definition_file_paths = %w(factories test/factories spec/factories)

but factory_girl_rails overrides with

Factory.definition_file_paths = [
  File.join(Rails.root, 'test', 'factories'),
  File.join(Rails.root, 'spec', 'factories')
]

I expect that moving from factory_girl to factory_girl_rails would not remove factories from the factory load path. Since factory_girl ships with cucumber steps, you shouldn't have to embed them in the spec folder since they're also used with the cucumber features.

Factories block migrations

Hello,
I would like to include factory_girl_rails also in the development environment in order to have access to factories from my development console.
However, I am just running

rake db:migrate

and I get a stack trace that one of my factories requires a "role" while the Roles table does not yet exist.

Table 'foo_development.roles' doesn't exist
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:312:in query' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:312:inexecute'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:462:in columns' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:685:incolumns'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:698:in column_names' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:711:incolumn_methods_hash'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:1041:in all_attributes_exists?' /Users/mulder/projects/foo/spec/factories/bar.rb:48:inall?'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:1040:in each' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:1040:inall?'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:1040:in all_attributes_exists?' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activerecord-3.0.10/lib/active_record/base.rb:989:inmethod_missing'
/Users/mulder/projects/foo/spec/factories/bar.rb:42
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl-2.1.0/lib/factory_girl/syntax/vintage.rb:30:in define' /Users/mulder/projects/foo/spec/factories/bar.rb:40 /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:235:inload'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:235:in load' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:225:inload_dependency'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:593:in new_constants_in' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:225:inload_dependency'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:235:in load' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:20:infind_definitions'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:19:in each' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:19:infind_definitions'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:15:in each' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:15:infind_definitions'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/factory_girl_rails-1.2.0/lib/factory_girl_rails/railtie.rb:12
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/lazy_load_hooks.rb:34:in call' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/lazy_load_hooks.rb:34:inexecute_hook'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/lazy_load_hooks.rb:43:in run_load_hooks' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/lazy_load_hooks.rb:42:ineach'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/lazy_load_hooks.rb:42:in run_load_hooks' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/application/finisher.rb:46 /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/initializable.rb:25:ininstance_exec'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/initializable.rb:25:in run' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/initializable.rb:50:inrun_initializers'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/initializable.rb:49:in each' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/initializable.rb:49:inrun_initializers'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/application.rb:134:in initialize!' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/application.rb:77:insend'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/application.rb:77:in method_missing' /Users/mulder/projects/foo/config/environment.rb:5 /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:239:inrequire'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:239:in require' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:225:inload_dependency'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:593:in new_constants_in' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:225:inload_dependency'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:239:in require' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/application.rb:103:inrequire_environment!'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/railties-3.0.10/lib/rails/application.rb:218:in initialize_tasks' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:636:incall'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:636:in execute' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:631:ineach'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:631:in execute' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:597:ininvoke_with_call_chain'
/Users/mulder/.rvm/rubies/ruby-1.8.7-p174/lib/ruby/1.8/monitor.rb:242:in synchronize' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:607:in invoke_prerequisites' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:604:ineach'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:604:in invoke_prerequisites' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:596:ininvoke_with_call_chain'
/Users/mulder/.rvm/rubies/ruby-1.8.7-p174/lib/ruby/1.8/monitor.rb:242:in synchronize' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:583:in invoke' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2051:ininvoke_task'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2029:ineach'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2023:in top_level' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2001:inrun'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/lib/rake.rb:1998:inrun'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/gems/rake-0.8.7/bin/rake:31
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/bin/rake:19:in `load'
/Users/mulder/.rvm/gems/ruby-1.8.7-p174@foo/bin/rake:19

Observer not getting called when I use factory

Factory(:questionnaire_invite_epistle, :resource => Application.first)
=> #<Epistle id: 2, mail_encoded: " I am pretend mail encoded ", created_at: "2011-06-19 01:26:54", updated_at: "2011-06-19 01:26:54", type: "QuestionnaireInviteEpistle", resource_id: 1, resource_type: "Application">

QuestionnaireInviteEpistle.create!(:resource => Application.first, :mail_encoded => 'hi')
222
1111
55555
55555999999
NameError: uninitialized constant QuestionnaireInviteEpistleObserver::FroggerInstrumentation
from /Users/o_o/Work/cr/fern_gully/app/observers/questionnaire_invite_epistle_observer.rb:19:in record_if_duplicate' from /Users/o_o/Work/cr/fern_gully/app/observers/questionnaire_invite_epistle_observer.rb:7:inafter_create'

[SOVLED] undefined method `definition_file_paths=' for FactoryGirl:Module

Hi,

This issue I ran into is logged here, for posterity. Hopefully, it will help someone out...

After installing factory_girl_rails (via Bundler, referencing git repo), I got the following error when running 'rake':

(in /home/david/Documents/dev/aladdin)
/home/david/.bundler/ruby/1.8/factory_girl_rails-6e523f0ed79d/lib/factory_girl_rails/railtie.rb:7: undefined method definition_file_paths=' for FactoryGirl:Module (NoMethodError) from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:34:incall'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:34:in execute_hook' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:43:inrun_load_hooks'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:42:in each' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:42:inrun_load_hooks'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/application/finisher.rb:46
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/initializable.rb:25:in instance_exec' from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/initializable.rb:25:inrun'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/initializable.rb:50:in run_initializers' from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/initializable.rb:49:ineach'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/initializable.rb:49:in run_initializers' from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/application.rb:134:ininitialize!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/application.rb:77:in send' from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/application.rb:77:inmethod_missing'
from /home/david/Documents/dev/aladdin/config/environment.rb:5
from ./test/test_helper.rb:2:in require' from ./test/test_helper.rb:2 from ./test/functional/users_controller_test.rb:1:inrequire'
from ./test/functional/users_controller_test.rb:1
from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in load' from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5 from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:ineach'
from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
Errors running test:units, test:functionals!

This goes away after specifically adding factory_girl (i.e. in addition to factory_girl_rails) in Bundler's Gemfile (also referencing git repo).

Move factory_girl generators from rails3-generators to this gem

Is there a reason that we couldn't move the factory_girl generators from rails3-generators into this gem?

I chatted with indirect (the owner of rails3-generators) and he said that he'd be fine with moving them:
a) into factory_girl
b) if that's not an option then moving them into factory_girl_rails.

Rails STI

I have class Payment < ActiveRecord::Base and class Osmp < Payment.
When I osmp = Factory(:osmp) then I got error ActiveRecord::SubclassNotFound:

The single-table inheritance mechanism failed to locate the subclass: 'type'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Deal.inheritance_column to use another column for that information.

This is a bug or something I did not do so?

Validation ignored when using a factory

I am not a 100% sure if this is a factory_girl issue, but it does seem so.

My situation is the following: I have a model UserFile that belongs_to a Folder:

class UserFile < ActiveRecord::Base
  has_attached_file :attachment
  belongs_to :folder

  validates_attachment_presence :attachment
  validates_presence_of :folder_id

  def copy(target_folder)
    new_file = self.clone
    new_file.folder = target_folder
    new_file.save!
  end
end

The following test fails unexpectedly:

test 'cannot copy a file to anything other than a folder' do
  file1 = Factory(:user_file)
  file2 = Factory(:user_file)
  file3 = UserFile.find(file1.id)
  folder = Factory(:folder)

  # Should pass, but fails
  assert_raise(ActiveRecord::RecordInvalid) { file1.copy(nil) }

  # This is the same record, but DOES pass
  assert_raise(ActiveRecord::RecordInvalid) { file3.copy(nil) }

  assert_raise(ActiveRecord::AssociationTypeMismatch) { file1.copy('string') }
  assert_raise(ActiveRecord::AssociationTypeMismatch) { file1.copy(file2) }
  assert file1.copy(folder)
end

My factory looks like this:

Factory.define :user_file do |f|
  f.attachment { File.open("#{Rails.root}/test/fixtures/textfile.txt") }
  f.sequence(:attachment_file_name) { |i| "test#{i}.txt" }
  f.folder { Folder.find_or_create_by_name('Root folder') }
end

The validates_presence_of :folder_id is ignored when using a factory_girl factory, but when I do an ActiveRecord#find it DOES work. I was thinking it could have something to do with calling clone in the copy method.

Follow the -rails convention of rspec and cucumber

This is going to sound really lame but I was hoping that TB would follow suit and use the hyphenation convention that the RSpec team has been using for their Rails specific adapters. Any chance of making this happen considering how recently this was released?

fixture_replacement in generators does not work

application.rb:

config.generators do |g|
    g.fixture_replacement :factory_girl, :dir => "spec/factories"
...

leads to:

rails g model test dummy
...
invoke      factory_girl
Could not find "fixtures.3.rb" in any of your source paths.

If I remove the fixture_replacement the rails generators work again.

Running validation on model's attributes in a has_one polymorphic relationship

The model Matchup is part of a polymorphic has_one relationship.

class Matchup < ActiveRecord::Base
  belongs_to :matchupable, :polymorphic => true

  validates :matchupable_id, :matchupable_type, :presence => true,
    :if => Proc.new { |record| record.is_baseball? }
end

class BaseballMatchup < ActiveRecord::Base
  has_one :matchup, :as => :matchupable
end

When I run the following spec, the validates presence of returns false for the first spec. In version 2.3.2, the spec ran successfully. After updating to 3.3.0, it fails.

describe Matchup do

  specify { Matchup.build(matchupable_id: nil, matchupable_type: nil) .should be_valid } # fails
  specify { Matchup.build(matchupable_id: 1, matchupable_type: 'BaseballMatchup') .should be_valid } # passes

end

Doesn't seem like the Proc is being evaluated when the validation method is called. Any help would be appreciated.

Dependency pulls down rails 3.0.0.rc2

steven$ gem install factory_girl_rails
Successfully installed activesupport-3.0.0.rc2
Successfully installed activemodel-3.0.0.rc2
Successfully installed actionpack-3.0.0.rc2
Successfully installed activerecord-3.0.0.rc2
Successfully installed activeresource-3.0.0.rc2
Successfully installed actionmailer-3.0.0.rc2
Successfully installed railties-3.0.0.rc2

this looks like a side effect of the gemspec asking for '>= 3.0.0.beta4'

Output from `gem update` (3.0.0)

Installing ri documentation for factory_girl_rails-3.0.0...
Building YARD (yri) index for factory_girl_rails-3.0.0...
[error]: ParserSyntaxError: syntax error in `lib/generators/factory_girl/model/templates/fixtures.rb`:(4,14): syntax error, unexpected tOP_ASGN, expecting keyword_end
[error]: Stack trace:
    /home/adam/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/parser/ruby/ruby_parser.rb:517:in `on_parse_error'
    /home/adam/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/parser/ruby/ruby_parser.rb:49:in `parse'
    /home/adam/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/parser/ruby/ruby_parser.rb:49:in `parse'
    /home/adam/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/parser/ruby/ruby_parser.rb:15:in `parse'
    /home/adam/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/parser/source_parser.rb:438:in `parse'
    /home/adam/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/parser/source_parser.rb:361:in `parse_in_order'

Model and Test values not the same

Hey I have a really weird problem which does not make any sense to me.
My environment is Factory_Girl_Rails(1.2.0) on jruby 1.6.3, rails 3.0.10
What the issue is I create a dummy object with factory_girl and some how the objects attributes are different within the tests and in the model.
What I mean by it is in the test the value of an attribute is 4.5 and in the model the same attribute is 5.0 on the same object.

Can some help me to determine if its a active record problem or factory_girl or something else?

Thanks

No method error when creating factory object where factory sets a property of that object

Anytime I create a Factory object, I get this error where the factory sets a property on that object -- in this case 'name'.

businesses.rb

FactoryGirl.define do
  factory :business do

    factory :business_main do
      name "Dave's Toys"
    end
  end
end

businesses table

        Column        |            Type             |                        Modifiers                        
----------------------+-----------------------------+---------------------------------------------------------
 id                   | integer                     | not null default nextval('businesses_id_seq'::regclass)
 name                 | character varying(255)      | 
 address_line1        | character varying(255)      | 
 address_line2        | character varying(255)      | 
 city                 | character varying(255)      | 
 state                | character varying(255)      | 
 zip                  | character varying(255)      | 

business_spec.rb

...
business  = Factory(:business_main)
...

 # Failure/Error: business  = Factory(:business_main)
 # NoMethodError:       undefined method `name=' for #<Business:0x007fc991684590>

Using rails (3.1.3) 
Using factory_girl (2.3.2) 
Using factory_girl_rails (1.4.0)
Using rspec-core (2.7.1) 
Using rspec-expectations (2.7.0) 
Using rspec-mocks (2.7.0) 
Using rspec (2.7.0) 
Using rspec-rails (2.7.0) 

AssociationTypeMismatch using Capybara-Webkit and Spork

This issue is well-documented at Stack Overflow. The problem arises with this combination of things:

  • Using Capybara-Webkit (or maybe even just Capybara) for integration testing.
  • Using Spork for faster testing.
  • Setting config.cache_classes to false in the DRB test environment so that changes to classes are picked up under Spork.
  • Using FactoryGirl as a fixture replacement.

AssociationTypeMismatch is raised when attempting to use a Factory which sets associated models after classes have been reloaded by Rails. A number of solutions are presented on Stack Overflow which involve manually loading and reloading FactoryGirl in the Spork.each_run block in various ways, but I haven't personally seen any such combination of approaches actually work.

I'm not too keen on the class reloading processes of Rails, so my only recourse for now is to restart Spork whenever these errors pop up, or to remove my integration tests from the DRB environment. Neither solution is palatable. I would be interested to know if there is a workaround that actually works, or if there are plans to fix the issue. I'm more than happy to answer any questions about my environment to help with the legwork.

factory_girl_rails creates bad dependancies in rake :environment task

Factory definitions force active record to reflect models during the : environment rake task. This is a problem as the :environment task is a prerequisite for creating the tables in the first place. So, if you are using factory_girl_rails in a project and you have factories defined, you cannot create the DBs from scratch.

This is a particularly bad problem when on-boarding new developers to a project using factory_girl_rails as they are unable to setup the development environment without deleting the factories first.

Example...

  • rails generate model Person name:string
  • rake db:migrate
  • vim spec/factories.rb (add person factory)
  • rake db:drop
  • rake db:setup

Rake will now fail saying it cannot find the 'people' table.

ActiveRecord::RecordNotSaved

When i'm trying to create two models from factory_girl factories:
Factory(:user)
Factory(:user)
On a second line it throws:
ActiveRecord::RecordNotSaved

ruby 1.9.3-preview1 -> undefined method `sequence' for Factory:Module

Hello,
I tried ruby-1.9.3-preview1 on a project with Rails 3.1 app and factory_girl_rails from git master.
Getting this error:
..
.rvm/gems/ruby-1.9.3-preview1/gems/factory_girl-2.1.2/lib/factory_girl/deprecated.rb:7:in method_missing': undefined methodsequence' for Factory:Module (NoMethodError)
..

after switching back to 1.9.2 the problem goes away...

requires factory_girl ~> 1.3 - not available?

bundle install reports:
No compatible versions could be found for required dependencies:
factory_girl (~> 1.3, runtime) not found in any of the sources
required by factory_girl_rails (>= 0, runtime)
All possible versions of origin requirements conflict.

include FactoryGirl::Syntax::Methods, Cucumber, and controller inheritance

Here's an interesting collision that I don't have any suggestions how to fix. When adding this line to Cucumber's env.rb:

include FactoryGirl::Syntax::Methods

(as suggested by factory_girl to get the create and build methods without having to type FactoryGirl every time), it pollutes the controller's methods, and ends up making Rails think that #create is an internal_method (defined here if that action is inherited from a superclass.

I just reverted to using FactoryGirl.create until I can think of a better fix. I understand if this gets closed, since it's not really an issue that needs to be fixed, just sort of an edge case, but I thought I would bring it to your attention anyways.

factories loaded twice - already defined error

Short version: I was adding a new gem into my Gemfile which caused my Gemfile.lock to be updated. I'm no longer able to use rake or start-up rails because my factories are being included twice. After removing the new gem and trying different versions of gems, I was able to isolate the problem to the changes between these versions.

Fine
factory_girl (2.0.2)
factory_girl_rails (1.1.0)

Problems
factory_girl (2.1.0)
factory_girl_rails (1.2.0)

It's not clear to me how to debug this further. Any advice?

I am using rspec and am including the factories in my spec_helper. If I comment out everything in my spec_helper, I still have an issue.

Here is the output from caller.join("\n")

/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/Documents/rails/my_project/spec/factories/courses.rb:1:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `block in load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:19:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:19:in `block in find_definitions'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl_rails-1.2.0/lib/factory_girl_rails/railtie.rb:12:in `block in <class:Railtie>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:34:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:42:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application/finisher.rb:46:in `block in <module:Finisher>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `instance_exec'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `run'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:50:in `block in run_initializers'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `run_initializers'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:134:in `initialize!'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
/Users/me/Documents/rails/my_project/config/environment.rb:5:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:103:in `require_environment!'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:218:in `block in initialize_tasks'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/bin/rake:19:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/bin/rake:19:in `<main>

Dependency on database tables at define-time breaks rake

I installed the factory_girl_rails gem with some of my factories having explicit references to other models, e.g. seed data like a category or a user role:

    trait :manager_role do
      roles [ Role.find_by_name!("Manager") ]
    end

When I reset my development database, it prevents rake from running and reloading my database. I'm only using FactoryGirl in specs, not in development environment so I think this shouldn't even be loaded when running rake?

Excon::Errors::SocketError

HI,

I have written some basic tests and I get the following error message:

Failure/Error: profiles = FactoryGirl.create_list(:profile, 2)
Excon::Errors::SocketError:
getaddrinfo: nodename nor servname provided, or not known

For some reason, this only appears when testing the whole file. If I run 1 spec at a time, there are no errors.

Is this an issue with FactoryGirl?

Regards

Mark

rails 3.0.0 compatiblity

Currently doesn't work with Rails 3:

Bundler could not find compatible versions for gem "activesupport":
In Gemfile:
factory_girl_rails (= 1.0) depends on
activesupport (= 3.0.0.beta4)

Factory is not a class (TypeError)

In http://github.com/francois/rental_agency, I'm trying to use HEAD of rspec, factory_girl, factory_girl_rails and shoulda. I hit a snag:

$ rake spec
(in /Users/francois/Projects/rental_agency)
/Users/francois/.rvm/gems/ree-1.8.7-2010.02/bundler/gems/factory_girl_rails-338105c/lib/factory_girl_rails/railtie.rb:4: Factory is not a class (TypeError)
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/bundler/gems/factory_girl_rails-338105c/lib/factory_girl_rails.rb:1:in `require'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/bundler/gems/factory_girl_rails-338105c/lib/factory_girl_rails.rb:1
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler/runtime.rb:64:in `require'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler/runtime.rb:64:in `require'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler/runtime.rb:62:in `each'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler/runtime.rb:62:in `require'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler/runtime.rb:51:in `each'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler/runtime.rb:51:in `require'
        from /Users/francois/.rvm/gems/ree-1.8.7-2010.02/gems/bundler-1.0.0.rc.3/lib/bundler.rb:107:in `require'
        from /Users/francois/Projects/rental_agency/config/application.rb:7
        from /Users/francois/Projects/rental_agency/config/environment.rb:2:in `require'
        from /Users/francois/Projects/rental_agency/config/environment.rb:2
        from ./spec/spec_helper.rb:4:in `require'
        from ./spec/spec_helper.rb:4
        from ./spec/models/customers_spec.rb:1:in `require'
        from ./spec/models/customers_spec.rb:1

Dependency on database tables at define-time breaks rake

I installed the factory_girl_rails gem with some of my factories having explicit references to other models, e.g. seed data like a category or a user role:

    trait :manager_role do
      roles [ Role.find_by_name!("Manager") ]
    end

When I reset my development database, it prevents rake from running and reloading my database. I'm only using FactoryGirl in specs, not in development environment so I think this shouldn't even be loaded when running rake?

Support for namespaced models

Rails 3.1 engines are around the corner. The way I understand Rails 3.1 engines is that I will need to namespace my models. At least it appears to be the preferred method.

I have a Post class that looks like this:

ror_blog/app/models/blog/post.rb
module Blog
class Post < ActiveRecord::Base
has_many :comments
end
end

I removed the "module Blog" and everything works but I'd like factory girl to know if factory girl supports a namespaces?

Failure/Error: @post = Factory(:post)
LoadError:
Expected /projects/ror_blog/app/models/blog/post.rb to define Post

NameError: uninitialized constant FactoryGirl

It appears that factory_girl_rails is supposed to automatically load itself, but it does not in my environment. The only installation instructions say to add factory_girl_rails to the gemfile (in the test group). But this does not make FactoryGirl available to me.

I get the error "NameError: uninitialized constant FactoryGirl"

When I require 'factory_girl' in my spec_helper.rb file, that error goes away, and I get a new error: "ArgumentError: Factory not registered: user", even though the :user factory has been created.

When I add "FactoryGirl.find_definitions" to my spec_helper.rb file, then everything works correctly.

I am using factory_girl-3.3.0, factory_girl_rails-3.3.0, rspec-2.8.0, rails-3.0.4, ruby-1.9.2

If factory_girl_rails is supposed to automatically load itself, then if fails in my environment. If it isn't, then perhaps the installation instructions should be updated to include instructions on requiring the necessary files.

Invalid gemspec in 1.5.0

Invalid gemspec in [[my gem path]/specifications/factory_girl_rails-1.5.0.gemspec]: Illformed requirement ["#Syck::DefaultKey:0x00000101b478a8 3.0.7"]

Factory.stub doesn't stub rails timestamps

When I stub a model :

event = Factory.stub(:event)

I expect the stub to be acting like a real ActiveRecord model.
Anyway :

event.created_at 
=> nil

This should defaults to Time.now for me. Do you agree with that ?
thx

Add a CHANGELOG/History file

Currently, is there any summary of the changes between two versions? I didn't find such a thing (except for the git way).

Add fixture replacement config setting to factory_girl_rails.rb

Adding this line

config.fixture_replacement :factory_girl

to the factory_girl_rails.rb file would remove the need to add this line

g.fixture_replacement :factory_girl, :dir => 'spec/factories'

in the application.rb file.

RSpec-rails and haml-rails do this and it makes it nice to not have to edit the application.rb to set this up.

When I get some time I will take a crack at implementing this.

Thanks for factory_girl_rails and factory_girl.
Mike

disable model factory generation

i tend to keep all my factory definitions in spec/factories.rb VS a seperate file for each model. is there a configuration method to disable the model generator?

Feature Request: Allow creation of factory from existing model

It would be nice to allow

rails g factory_girl:model Post --from-schema

This would read the existing Post model's attributes and generate a fixture from there instead of having to specify the attributes explicitly on the command line. The use case allows for reducing the effort a bit when moving a largish project a lot of models (with lots of attributes) to factory_girl.

Ideas? Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.