Build Status Gem Version Dependency Status Code Climate

RomFactory

Installation

Add this line to your application's Gemfile:

gem 'rom_factory'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rom_factory

Configuration

First, you have to define ROM container:

container = ROM.container(:sql, 'sqlite::memory') do |conf|
  conf.default.create_table(:users) do
    primary_key :id
    column :last_name, String, null: false
    column :first_name, String, null: false
    column :email, String, null: false
    column :created_at, Time, null: false
    column :updated_at, Time, null: false
  end
end

Once that is done, you will have to specify which container RomFactory will use:

RomFactory::Config.configure do |config|
  config.container = container
end

Simple use case

After configuration is done, you can define the factory as follows:

RomFactory::Builder.define do |b|
  b.factory(relation: :users, name: :user) do |f|
    f.first_name "Janis"
    f.last_name "Miezitis"
    f.email "[email protected]"
  end
end

When everything is configured, you can use it in your tests as follows:

user = RomFactory::Builder.create(:user)
user.email #=> "[email protected]"

Callable properties

You can easily define dynamic (callbale) properties if value needs to change every time it needs to be called. Anything that responds to .call can be dynamic property.

RomFactory::Builder.define do |b|
  b.factory(relation: :users, name: :user) do |f|
    f.first_name "Janis"
    f.last_name "Miezitis"
    f.email "[email protected]"
    f.created_at {Time.now}
  end
end
user = RomFactory::Builder.create(:user)
user.created_at #=> 2016-08-27 18:17:08 -0500

Sequencing

If you want attributes to be unique each time you build a factory, you can use sequence to achieve that:

RomFactory::Builder.define do |b|
  b.factory(relation: :users, name: :user) do |f|
    f.first_name "Janis"
    f.last_name "Miezitis"
    f.sequence :email do |n|
      "janjiss#{n}@gmail.com"
    end
  end
end
user = RomFactory::Builder.create(:user)
user.email #=> [email protected]
user2 = RomFactory::Builder.create(:user)
user2.email #=> [email protected]

License

The gem is available as open source under the terms of the MIT License.