• Stars
    star
    5
  • Rank 2,768,509 (Top 57 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Let your factories use your business logic, making them flexible and easier to update.

Journeyman

Gem Version Code Climate Test Coverage Inline docs Build Status License

Journeyman is a fixtures replacement with an extremely light definition syntax, providing two default build strategies, but allowing full customization on how to build an object.

Usage

Journeyman is built to work out of the box with RSpec and Cucumber, and any console environment, if you need support for other testing frameworks we can work it out πŸ˜ƒ.

Since it has no dependencies, it's possible to use it in any Ruby project.

Load

Journeyman will attempt to load files under the spec/factories directory, but you may overwrite Journeyman.factories_paths by providing an Array that containst different paths for factories.

Definition

Journeyman allows you to provide the default attributes for creation as the return value of the definition block.

Journeyman.define :album do |t|
  {
    title: 'Wish You Were Here',
    recorded_ago: -> { (Date.today - Date.new(1975, 9, 12)).round / 365 },
    band: -> { Journeyman.create(:band, name: 'Pink Floyd') }
  }
end

The default values are superseded by the value you provide to build or create.

pink_floyd = find_band('Pink Floyd')

Journeyman.build(:album, band: pink_floyd) == Album.new({
  title: 'Wish You Were Here',
  recorded_ago: -> { ... }.call,
  band: pink_floyd
})

The default values can be static, or dynamic. If you specify a Proc or lambda as a default value for an attribute, it will be evaluated whenever the attribute is not provided when an object is built.

Configuration

Journeyman is a configurable beast, yet has really strong defaults.

# DSL Methods
find, process, ignore, build, after_create

# Configuration Options
[:parent, :model, :finder_attribute]

DSL

Journeyman provides a nice DSL that lets you provide a block or lambda with your own builder, or finder, and other miscellanous (and handy) hooks.

  • find: Allows you to define the finder, takes a single argument.

  • build: You can provide a custom builder, receives a Hash of attributes, but you have full liberty of what you do with them, the return value must be the built object.

  • process: If you need to process the attributes before building you can provide a block to do just that, make sure to return the attributes at the end.

  • ignore: There are cases where you want to ignore certain attributes during the build, but you want them in the after_create callback. You can ignore those attributes by passing a list, or Array with the attributes you wish to ignore.

  • after_create: Callback that takes the newly built object, and the original attributes. Specially useful when combined with ignore.

Journeyman.define :employee do |c|
  c.find { |id| Person.find(id) }

  c.build { |attrs|
    attrs.delete(:company).new_employee(attrs)
  }

  c.ignore :work_history

  c.after_create { |employee, attrs|
    attrs[:work_history].each do |history|
      check_references(history)
    end
  }
end

Configuration Options

Configuration options can be passed alongside the name in the factory definition:

  • parent: Name of the factory that is going to be used as a parent, if parent is set, the default builder consists of invoking the parent factory builder.

  • model: Expects a class that will be used for the default builder and finder. Useful for cases where the inferrence from the name does not work, or the factory name is simply different than the object class it's building.

  • finder_attribute: Name of the attribute used to find an object by the default finder. The default is :name.

Journeyman.define :employee, model: People, finder_attribute: :social_security_id do
...
end

Journeyman.define :journeyman, parent: :employee do
...
end

Setup

# Gemfile
group :test do
  gem 'journeyman'
end

# Generic Use (mock script)
require 'journeyman'

Journeyman.load(self)

RSpec

# spec/support/spec_helper.rb or similar
require 'journeyman'

Journeyman.load(self, framework: :rspec)

Cucumber

# features/support/journeyman.rb or similar
require 'journeyman'

Journeyman.load(self, framework: :cucumber)

Advantages

  • You have full control of how your objects are created, and have to write less boilerplate.
  • You can chain several factories using parent, which allows you to create different factories for the same object with less effort.
  • Code is highly optimized, so the library is much faster than say, FactoryGirl, specially when building objects without database interaction.

Examples

You can check the specs of the project to check how to check some basic factories, and learn how to set it up.

Notes

  • The DSL does not use instance_exec to allow access to the external context.

License

Copyright (c) 2014 MΓ‘ximo Mussini

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.

More Repositories

1

vite_ruby

⚑️ Vite.js in Ruby, bringing joy to your JavaScript experience
Ruby
1,124
star
2

iles

🏝 The joyful site generator
TypeScript
1,043
star
3

vite-plugin-image-presets

πŸ–Ό Image Presets for Vite.js apps
TypeScript
243
star
4

vite-plugin-environment

Easily expose environment variables in Vite.js
TypeScript
132
star
5

vite-plugin-full-reload

♻️ Automatically reload the page when files are modified
JavaScript
121
star
6

oj_serializers

⚑️ Faster JSON serialization for Ruby on Rails. Easily migrate away from Active Model Serializers.
Ruby
98
star
7

js_from_routes

πŸ›£οΈ Generate path helpers and API methods from your Rails routes
Ruby
86
star
8

request_store_rails

πŸ“¦ Per-request global storage for Rails prepared for multi-threaded apps
Ruby
83
star
9

types_from_serializers

βœ… Generate TypeScript interfaces from your JSON serializers
Ruby
71
star
10

vuex-stores

πŸ—„ Store objects for Vuex, a simple and more fluid API for state-management.
JavaScript
63
star
11

vue-custom-element-example

An example on how to define custom elements using Vue 3
TypeScript
54
star
12

mongoid_includes

🌿 Improves eager loading support for Mongoid
Ruby
46
star
13

jekyll-vite

⚑️🩸 Use Vite.js in Jekyll as your assets pipeline
Ruby
44
star
14

queryable

❔ Gives your queries a home and avoid tucking scopes inside your models
Ruby
42
star
15

vite-plugin-stimulus-hmr

⚑️ HMR for Stimulus controllers in Vite.js
TypeScript
42
star
16

stimulus-vite-helpers

Helpers to easily load all your Stimulus controllers when using Vite.js
TypeScript
37
star
17

capybara-compose

βœ… Easily write fluent integration tests with Capybara in Ruby
Ruby
31
star
18

better_settings

βš™ Settings for Ruby apps – fast, immutable, better.
Ruby
20
star
19

vite-plugin-bugsnag

Report builds and upload source maps to Bugsnag
TypeScript
18
star
20

i18n_multitenant

🌎 Provides a convenient way to use tenant-specific translations
Ruby
16
star
21

vite-plugin-manifest-sri

Subresource Integrity for Vite.js manifest files
JavaScript
13
star
22

resourcerer

✨ Works like magic to dry up your controllers
Ruby
10
star
23

sublime-toggle-dark-mode

🌚🌞 Toggle between dark and light mode in Sublime Text 4
JavaScript
9
star
24

pakiderm

🐘 Pakiderm will never forget the return value
Ruby
7
star
25

presenter_rails

πŸ”­ Expose your view models in a convenient way
Ruby
6
star
26

vite-plugin-erb

Use ERB files in Vite.js projects with a Ruby backend
TypeScript
5
star
27

jekyll-vite-minima

⚑️🩸 Use Vite.js in Jekyll minima theme as your assets pipeline
Ruby
3
star
28

automatic-music-transcription

Automatically exported from code.google.com
C
2
star
29

vite-plugin-xdm

Use XDM in VIte.js
JavaScript
2
star
30

crouton

🍞 Context sensitive notifications for Rails
Ruby
1
star
31

fast-food-mvc

Automatically exported from code.google.com
C#
1
star
32

ElMassimo

1
star
33

vite-vue-router-hmr-repro

Vue
1
star