diff --git a/lib/factory_bot/definition.rb b/lib/factory_bot/definition.rb index 4aded74..ab6b341 100644 --- a/lib/factory_bot/definition.rb +++ b/lib/factory_bot/definition.rb @@ -30,7 +30,7 @@ module FactoryBot end def to_create(&block) - if block_given? + if block @to_create = block else aggregate_from_traits_and_self(:to_create) { @to_create }.last diff --git a/lib/factory_bot/registry.rb b/lib/factory_bot/registry.rb index ca55787..c7eb039 100644 --- a/lib/factory_bot/registry.rb +++ b/lib/factory_bot/registry.rb @@ -25,7 +25,7 @@ module FactoryBot raise key_error_with_custom_message(e) end - alias [] find + alias_method :[], :find def register(name, item) @items[name] = item diff --git a/lib/factory_bot/syntax/default.rb b/lib/factory_bot/syntax/default.rb index 0ef7608..fd391d0 100644 --- a/lib/factory_bot/syntax/default.rb +++ b/lib/factory_bot/syntax/default.rb @@ -15,7 +15,7 @@ module FactoryBot def factory(name, options = {}, &block) factory = Factory.new(name, options) proxy = FactoryBot::DefinitionProxy.new(factory.definition) - proxy.instance_eval(&block) if block_given? + proxy.instance_eval(&block) if block Internal.register_factory(factory) diff --git a/lib/factory_bot/trait.rb b/lib/factory_bot/trait.rb index 944eee7..710783b 100644 --- a/lib/factory_bot/trait.rb +++ b/lib/factory_bot/trait.rb @@ -9,7 +9,7 @@ module FactoryBot @definition = Definition.new(@name) proxy = FactoryBot::DefinitionProxy.new(@definition) - if block_given? + if block proxy.instance_eval(&@block) end end diff --git a/spec/factory_bot/attribute/association_spec.rb b/spec/factory_bot/attribute/association_spec.rb index 4d54871..b9bcfac 100644 --- a/spec/factory_bot/attribute/association_spec.rb +++ b/spec/factory_bot/attribute/association_spec.rb @@ -6,15 +6,14 @@ describe FactoryBot::Attribute::Association do subject { FactoryBot::Attribute::Association.new(name, factory, overrides) } - module MissingMethods - def association(*args) - end - end - before do # Define an '#association' instance method allowing it to be mocked. # Ususually this is determined via '#method_missing' - subject.extend(MissingMethods) + missing_methods = Module.new { + def association(*args) + end + } + subject.extend(missing_methods) allow(subject) .to receive(:association).with(any_args).and_return association diff --git a/spec/factory_bot/attribute/dynamic_spec.rb b/spec/factory_bot/attribute/dynamic_spec.rb index 60cfe09..bec6627 100644 --- a/spec/factory_bot/attribute/dynamic_spec.rb +++ b/spec/factory_bot/attribute/dynamic_spec.rb @@ -26,15 +26,14 @@ describe FactoryBot::Attribute::Dynamic do let(:block) { -> { attribute_defined_on_attribute } } let(:result) { "other attribute value" } - module MissingMethods - def attribute_defined_on_attribute(*args) - end - end - before do # Define an '#attribute_defined_on_attribute' instance method allowing it # be mocked. Ususually this is determined via '#method_missing' - subject.extend(MissingMethods) + missing_methods = Module.new { + def attribute_defined_on_attribute(*args) + end + } + subject.extend(missing_methods) allow(subject) .to receive(:attribute_defined_on_attribute).and_return result diff --git a/spec/support/macros/define_constant.rb b/spec/support/macros/define_constant.rb index b8c4b10..2621dd1 100644 --- a/spec/support/macros/define_constant.rb +++ b/spec/support/macros/define_constant.rb @@ -3,7 +3,7 @@ require "active_record" module DefineConstantMacros def define_class(path, base = Object, &block) const = stub_const(path, Class.new(base)) - const.class_eval(&block) if block_given? + const.class_eval(&block) if block const end