Introduction

Seed introduces an easy way to instantiate factories created with Factory Girl.

Install

sudo gem install factory_seed

If you want the source go to github.com/fnando/factory_seed

Usage

Take the following factories:

Factory.define(:user) do |f|
  f.username { Factory.next(:username) }
  f.password "test"
  f.password_confirmation "test"
end

Factory.define(:messages) do |f|
  f.association :user
  f.subject "Some subject"
end

Factory.define(:comments) do |f|
  f.association :user
  f.comment "Some comment"
end

You need to include the Seed module where you want to use it.

require "seed"
include Seed

_10_users
_1_user_with_10_comments
_1_user_with_10_comments_and_3_messages

If you think that underscores are ugly, you can simply use the seed method.

seed "10 users"
seed "1 user with 10 comments"

You can set variables by using the records you just created.

@user = _1_user
@user, @comments = _1_user_with_10_comments
@user, @comments, @messages = _1_user_with_10_comments_and_10_messages

You can define the associations by providing a hash.

@user = _1_user
_10_comments(:user => @user)

In fact, you can set any attribute by providing this hash.

@user = _1_user(:username => "johndoe")

If you need to set attributes from multiple factories, the best approach is to break into several calls.

@user = _1_user(:username => "johndoe")
@comments = _10_messages(:user => @user, :subject => "Welcome to our App")

The seed method can receive a hash as well.

@user = seed("1 user", :username => "johndoe")

Using with Test::Unit

Just include the Seed module.

class SomeTest < Test::Unit::TestCase
  include Seed
end

Or if you want to make Seed available to all test cases

class Test::Unit::TestCase
  include Seed
end

Using with RSpec

Include the Seed module by using the configure method.

Spec::Runner.configure do |config|
  config.include(Seed)
end

License

(The MIT License)

Copyright © 2010:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.