Skip to content

stephencelis/minifacture

Repository files navigation

Minifacture

factory_girl for minitest.

Factory.define :user do |f|
  f.name 'John Doe'                            # String.
  f.login 'john%d'                             # Sequence.
  f.email '%{login}@example.com'               # Interpolate.
  f.password f.password_confirmation('foobar') # Chain.
  f.active { [true,false].sample }             # Block.
  f.status { |f| f.active ? "On" : "Off" }     # Block with object parameter.
end

Factory.define :post do |f|
  f.user { Factory :user }                     # Association.
end

Install

# Gemfile
group :test do
  gem 'minifacture'
end
$ bundle
# test/test_helper.rb
require 'factories' # If you define your factories in test/factories.rb

Use

To get a User instance that's not saved:

u = Factory.build :user

To get a User instance that's saved:

u = Factory.create :user

Shorthand for create:

u = Factory :user

Methods

Methods can customize a factory's object as it is being built.

Take a simple class:

class User
  attr_accessor :name
  attr_accessor :email
end

And a simple factory:

Factory.define :user do
end

Use:

u = Factory :user, name: 'Alice', email: '[email protected]'
u.name  # => "Alice"
u.email # => "[email protected]"

This initializes the object, then calls the object's name= method and email= method.

Blocks

Blocks can use any Ruby code:

Factory.define :user do |f|
  f.name { %w[Alice Bob Carol].sample }
  f.email { "#{%w[alice bob carol].sample}@example.com" }
end

Use:

u = Factory :user
u.name  # => "Bob"
u.email # => "[email protected]"

Blocks can use a parameter to access the object as it is being built:

Factory.define :user do |f|
  f.name { %w[Alice Bob Carol].choice }
  f.email { |u| "#{u.name.downcase}@example.com" }
end

Use:

u = Factory :user
u.name  # => "Bob"
u.email # => "[email protected]"

Methods + Blocks

When you use methods and blocks together, the methods are called, then the blocks:

user = Factory :user, name: 'Eve'
user.name  # => "Eve"
user.email # => "[email protected]"

This initializes the object, then calls the object's name= method, then processes the factory attribute email which calls the block.

Namespacing

If your model is namespaced, or does not correspond directly with the symbol, you can explicitly declare which class to use:

Factory.define :post, class: BlogEngine::Post do
  # ...
end

Parent

A factory can use a parent factory:

Factory.define :user, do |f|
  f.name "Alice"
end

Factory.define :admin, :parent => :user do |f|
  f.role "Administrator"
end

When you use the factory, the result combines the parent factory settings:

Factory :admin
=> User with name="Alice" and role="Administrator"

The factory can overwrite the parent attributes:

Factory.define :user, do |f|
  f.name "Alice"
  f.role "Guest"
end

Factory.define :admin, :parent => :user do |f|
  f.role "Administrator"  # Replaces the parent factory role
end

License

(The MIT License)

© 2010–2014 Stephen Celis [email protected].

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.

About

factory_girl for minitest

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages