-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce optional FactoryGirl syntax that makes it easier to use Formulaic to fill in attributes from a factory with a single argument: `fill_with_factory(:user)`; limit the attributes from a factory: `fill_with_factory(:user, :name, :address)`; and even pass arguments to the factory, such as traits and overrides: `fill_with_factory(:user, :name, :address, factory_arguments: [:with_verified_address]`. Resolves #11.
- Loading branch information
1 parent
f48ee1d
commit eade5c9
Showing
5 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'formulaic/dsl' | ||
require 'factory_girl' | ||
require 'active_support/concern' | ||
|
||
module Formulaic | ||
module FactoryGirl | ||
include ::FactoryGirl::Syntax::Methods | ||
def fill_with_factory(model_name, *attribute_whitelist, factory_arguments: []) | ||
attributes = attributes_for(model_name, *factory_arguments) | ||
|
||
if attribute_whitelist.any? | ||
attributes = attributes.slice(*attribute_whitelist) | ||
end | ||
|
||
fill_form(model_name, attributes) | ||
end | ||
end | ||
|
||
Formulaic::Dsl.send(:include, Formulaic::FactoryGirl) | ||
end | ||
|
||
#fill_with_factory(:user, :name, :age, factory_arguments: [:authenticated, :super_old, name: 'george']) | ||
#=> | ||
#Formulaic::Dsl.fill_form(FactoryGirl.attributes_for(:user, :authenticated, :super_old, name: 'george').slice(:name, :age)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
require 'formulaic/factory_girl' | ||
|
||
describe Formulaic::FactoryGirl, :factory_girl do | ||
before(:all) do | ||
define_user_factory(user_class) | ||
end | ||
|
||
after(:all) do | ||
FactoryGirl.factories.clear | ||
end | ||
|
||
it 'sets attributes to FactoryGirl.attributes_for(model_name) if attributes is omitted' do | ||
allow(class_with_dsl).to receive(:fill_form) | ||
|
||
fill_with_factory(:user) | ||
|
||
expect(class_with_dsl).to have_received(:fill_form).with(:user, FactoryGirl.attributes_for(:user)) | ||
end | ||
|
||
it 'slices the attributes if an attribute whitelist is included' do | ||
allow(class_with_dsl).to receive(:fill_form) | ||
|
||
fill_with_factory(:user, :name) | ||
|
||
expect(class_with_dsl).to have_received(:fill_form).with(:user, FactoryGirl.attributes_for(:user).slice(:name)) | ||
end | ||
|
||
it 'merges overrides into the attributes' do | ||
allow(class_with_dsl).to receive(:fill_form) | ||
|
||
fill_with_factory :user, :name, factory_arguments: [name: 'Tao' ] | ||
|
||
expect(class_with_dsl).to have_received(:fill_form).with(:user, { name: 'Tao' }) | ||
end | ||
|
||
delegate :fill_with_factory, to: :class_with_dsl | ||
|
||
def class_with_dsl | ||
@class ||= Class.new { include Formulaic::Dsl }.new | ||
end | ||
|
||
def user_class | ||
@user ||= Class.new do | ||
attr_accessor :name, :age | ||
|
||
def initialize(options) | ||
@name = options[:name] | ||
@age = options[:age] | ||
end | ||
end | ||
end | ||
|
||
def define_user_factory(user_class) | ||
FactoryGirl.define do | ||
factory :user, class: user_class do | ||
name 'Roen Tan' | ||
age 31 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters