Skip to content

Instantly share code, notes, and snippets.

@jonduarte
Last active December 21, 2017 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonduarte/dff29d4b7375fb12e91f485c65661f06 to your computer and use it in GitHub Desktop.
Save jonduarte/dff29d4b7375fb12e91f485c65661f06 to your computer and use it in GitHub Desktop.
How to fabricate hashes and open structures
# spec/fabricators/stuff.rb
Fabricator(:one_stuff, from: :open_struct) do
name 'Pedro'
end
Fabricator(:second_stuff, from: :hash) do
after_build do |s|
s['name'] = 'Jose'
end
end
# spec/models/example_spec.rb
require 'spec_helper'
require 'fabrication'
describe 'A class' do
let(:something) { Fabricate.build(:one_stuff) }
let(:elsei) { Fabricate.build(:second_stuff) }
it 'is true' do
expect(something.name).to eq('Pedro')
expect(elsei).to eq({ 'name' => 'Jose' })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment