Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Preload factories (Factory Girl) just like fixtures. It will be easy and, probably, faster!
Ruby
tag: v0.1.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
.rspec
Gemfile
Gemfile.lock
README.rdoc
Rakefile
factory_girl-preload.gemspec

README.rdoc

factory_girl-preload

We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters Factory Girl (FG).

Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of specs.

So here enters Factory Girl Preload (FGP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).

Installation

gem install factory_girl-preload

Usage

I'm focusing Rails 3 + RSpec 2 stack, so I can't really guarantee that it will work on other setups. Here's how you get started:

Add both FG and FGP to your Gemfile:

source :rubygems
gem "rails", "3.0.6"
gem "mysql2", "~> 0.2.7"

group :test, :development do
  gem "rspec-rails", "~> 2.5.0"
  gem "factory_girl", "~> 1.3.3"
  gem "factory_girl-preload", "~> 0.1.0"
end

On spec/spec_helper.rb file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.mock_with :rspec
end

Create your fixtures on spec/support/factories.rb. You may have something like this:

Factory.sequence :email do |n|
  "john#{n}@doe.com"
end

Factory.sequence :username do |n|
  "john#{n}"
end

Factory.sequence :public_key do |n|
  File.read(Rails.root.join("spec/fixtures/hash.rsa")) + n.to_s
end

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

Factory.define :projects do |f|
  f.name "My Project"
  f.association :user
end

Finally, you can create your preloadable factories. Put those on spec/support/preloadable_factories.rb:

Factory.preload do
  factory(:john) { Factory(:user) }
  factory(:myapp) { Factory(:project, :user => users(:john)) }
end

Like Rails fixtures, FGP will define methods for each model. You can use it on your examples and alike.

require "spec_helper"

describe User do
  let(:user) { users(:john) }

  it "returns john's record" do
    users(:john).should be_an(User)
  end

  it "returns myapp's record" do
    projects(:myapp).user.should == users(:john)
  end
end

Easy and, probably, faster!

Maintainer

License

(The MIT License)

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.

Something went wrong with that request. Please try again.