mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Pass evaluator to to_create (#1036)
This commit is contained in:
parent
3872ab7d0f
commit
5e6f018c58
3 changed files with 30 additions and 3 deletions
|
@ -4,7 +4,8 @@ module FactoryGirl
|
|||
class Evaluation
|
||||
include Observable
|
||||
|
||||
def initialize(attribute_assigner, to_create)
|
||||
def initialize(evaluator, attribute_assigner, to_create)
|
||||
@evaluator = evaluator
|
||||
@attribute_assigner = attribute_assigner
|
||||
@to_create = to_create
|
||||
end
|
||||
|
@ -12,7 +13,10 @@ module FactoryGirl
|
|||
delegate :object, :hash, to: :@attribute_assigner
|
||||
|
||||
def create(result_instance)
|
||||
@to_create[result_instance]
|
||||
case @to_create.arity
|
||||
when 2 then @to_create[result_instance, @evaluator]
|
||||
else @to_create[result_instance]
|
||||
end
|
||||
end
|
||||
|
||||
def notify(name, result_instance)
|
||||
|
|
|
@ -36,7 +36,8 @@ module FactoryGirl
|
|||
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
|
||||
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)
|
||||
|
||||
evaluation = Evaluation.new(attribute_assigner, compiled_to_create)
|
||||
evaluation =
|
||||
Evaluation.new(evaluator, attribute_assigner, compiled_to_create)
|
||||
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
|
||||
|
||||
strategy.result(evaluation).tap(&block)
|
||||
|
|
|
@ -88,6 +88,28 @@ describe "a custom create" do
|
|||
end
|
||||
end
|
||||
|
||||
describe "a custom create passing in an evaluator" do
|
||||
before do
|
||||
define_class("User") do
|
||||
attr_accessor :name
|
||||
end
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :user do
|
||||
transient { creation_name "evaluator" }
|
||||
|
||||
to_create do |user, evaluator|
|
||||
user.name = evaluator.creation_name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "passes the evaluator to the custom create block" do
|
||||
expect(FactoryGirl.create(:user).name).to eq "evaluator"
|
||||
end
|
||||
end
|
||||
|
||||
describe "calling `create` with a block" do
|
||||
include FactoryGirl::Syntax::Methods
|
||||
|
||||
|
|
Loading…
Reference in a new issue