<img src=“https://codeclimate.com/github/mattways/rails_factory.png” /> <img src=“https://travis-ci.org/mattways/rails_factory.png?branch=master” alt=“Build Status” /> <img src=“https://gemnasium.com/mattways/rails_factory.png” alt=“Dependency Status” />

Rails Factory

Minimalistic factory to replace fixtures.

Install

Put this line in your Gemfile:

gem 'rails_factory'

Then bundle:

$ bundle

Usage

Put the DSL in your test_helper or in some other file:

Factory.define do
  # Your definitions
end

Inside the DSL you can use attributes method to define the defaults:

attributes :model do
  { :attr => 'value' }
end

You can use before_create method to tweak the instance too:

before_create :model do |instance|
  instance.attr = 'value'
end

Then just call the corresponding methods in your tests:

To retrieve a hash:

Factory.model_hash

To retrieve a new instance:

Factory.new_model

To retrieve a created instance:

Factory.create_model

In all the cases you can override the defaults:

Factory.model_hash :attr => 'value'