Remove static attributes from the definition proxy
This changes the signature of `add_attribute`, so if you pass a value at all you will now get an `ArgumentError`. Any `method_missing`-style static attribute definitions will become `NoMethodError`s.
This commit is contained in:
parent
bf04aaa068
commit
805933eed2
|
@ -39,16 +39,8 @@ module FactoryBot
|
|||
# generated instances.
|
||||
# * value: +Object+
|
||||
# If no block is given, this value will be used for this attribute.
|
||||
def add_attribute(name, value = nil, &block)
|
||||
raise AttributeDefinitionError, 'Both value and block given' if value && block_given?
|
||||
|
||||
declaration = if block_given?
|
||||
Declaration::Dynamic.new(name, @ignore, block)
|
||||
else
|
||||
warn_static_attribute_deprecation(name, value)
|
||||
Declaration::Static.new(name, value, @ignore)
|
||||
end
|
||||
|
||||
def add_attribute(name, &block)
|
||||
declaration = Declaration::Dynamic.new(name, @ignore, block)
|
||||
@definition.declare_attribute(declaration)
|
||||
end
|
||||
|
||||
|
@ -61,13 +53,13 @@ module FactoryBot
|
|||
# attribute, so that:
|
||||
#
|
||||
# factory :user do
|
||||
# name 'Billy Idol'
|
||||
# name { 'Billy Idol' }
|
||||
# end
|
||||
#
|
||||
# and:
|
||||
#
|
||||
# factory :user do
|
||||
# add_attribute :name, 'Billy Idol'
|
||||
# add_attribute(:name) { 'Billy Idol' }
|
||||
# end
|
||||
#
|
||||
# are equivalent.
|
||||
|
@ -89,12 +81,12 @@ module FactoryBot
|
|||
#
|
||||
# are equivalent.
|
||||
def method_missing(name, *args, &block)
|
||||
if args.empty? && block.nil?
|
||||
@definition.declare_attribute(Declaration::Implicit.new(name, @definition, @ignore))
|
||||
if args.empty?
|
||||
__declare_attribute__(name, block)
|
||||
elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory)
|
||||
association(name, *args)
|
||||
else
|
||||
add_attribute(name, *args, &block)
|
||||
super(name, *args, &block)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -170,28 +162,13 @@ module FactoryBot
|
|||
|
||||
private
|
||||
|
||||
def warn_static_attribute_deprecation(name, value)
|
||||
attribute_caller = caller(2)
|
||||
|
||||
if attribute_caller[0].include?("method_missing")
|
||||
attribute_caller = caller(3)
|
||||
def __declare_attribute__(name, block)
|
||||
if block.nil?
|
||||
declaration = Declaration::Implicit.new(name, @definition, @ignore)
|
||||
@definition.declare_attribute(declaration)
|
||||
else
|
||||
add_attribute(name, &block)
|
||||
end
|
||||
|
||||
ActiveSupport::Deprecation.warn(<<-MSG, attribute_caller)
|
||||
Static attributes will be removed in FactoryBot 5.0. Please use dynamic
|
||||
attributes instead by wrapping the attribute value in a block:
|
||||
|
||||
#{name} { #{value.inspect} }
|
||||
|
||||
To automatically update from static attributes to dynamic ones,
|
||||
install rubocop-rspec and run:
|
||||
|
||||
rubocop \\
|
||||
--require rubocop-rspec \\
|
||||
--only FactoryBot/AttributeDefinedStatically \\
|
||||
--auto-correct
|
||||
|
||||
MSG
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,17 +2,6 @@ describe FactoryBot::DefinitionProxy, "#add_attribute" do
|
|||
subject { FactoryBot::Definition.new(:name) }
|
||||
let(:proxy) { FactoryBot::DefinitionProxy.new(subject) }
|
||||
|
||||
it "raises if both a block and value are given" do
|
||||
expect {
|
||||
proxy.add_attribute(:something, "great") { "will raise!" }
|
||||
}.to raise_error(FactoryBot::AttributeDefinitionError, "Both value and block given")
|
||||
end
|
||||
|
||||
it "declares a static attribute on the factory" do
|
||||
proxy.add_attribute(:attribute_name, "attribute value")
|
||||
expect(subject).to have_static_declaration(:attribute_name).with_value("attribute value")
|
||||
end
|
||||
|
||||
it "declares a dynamic attribute on the factory" do
|
||||
attribute_value = -> { "dynamic attribute" }
|
||||
proxy.add_attribute(:attribute_name, &attribute_value)
|
||||
|
@ -24,17 +13,6 @@ describe FactoryBot::DefinitionProxy, "#add_attribute when the proxy ignores att
|
|||
subject { FactoryBot::Definition.new(:name) }
|
||||
let(:proxy) { FactoryBot::DefinitionProxy.new(subject, true) }
|
||||
|
||||
it "raises if both a block and value are given" do
|
||||
expect {
|
||||
proxy.add_attribute(:something, "great") { "will raise!" }
|
||||
}.to raise_error(FactoryBot::AttributeDefinitionError, "Both value and block given")
|
||||
end
|
||||
|
||||
it "declares a static attribute on the factory" do
|
||||
proxy.add_attribute(:attribute_name, "attribute value")
|
||||
expect(subject).to have_static_declaration(:attribute_name).ignored.with_value("attribute value")
|
||||
end
|
||||
|
||||
it "declares a dynamic attribute on the factory" do
|
||||
attribute_value = -> { "dynamic attribute" }
|
||||
proxy.add_attribute(:attribute_name, &attribute_value)
|
||||
|
@ -47,11 +25,12 @@ describe FactoryBot::DefinitionProxy, "#transient" do
|
|||
let(:proxy) { FactoryBot::DefinitionProxy.new(subject) }
|
||||
|
||||
it "makes all attributes added ignored" do
|
||||
attribute_value = -> { "dynamic_attribute" }
|
||||
proxy.transient do
|
||||
add_attribute(:attribute_name, "attribute value")
|
||||
add_attribute(:attribute_name, &attribute_value)
|
||||
end
|
||||
|
||||
expect(subject).to have_static_declaration(:attribute_name).ignored.with_value("attribute value")
|
||||
expect(subject).to have_dynamic_declaration(:attribute_name).ignored.with_value(attribute_value)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -69,16 +48,16 @@ describe FactoryBot::DefinitionProxy, "#method_missing" do
|
|||
expect(subject).to have_association_declaration(:author).with_options(factory: :user)
|
||||
end
|
||||
|
||||
it "declares a static attribute" do
|
||||
proxy.attribute_name "attribute value"
|
||||
expect(subject).to have_static_declaration(:attribute_name).with_value("attribute value")
|
||||
end
|
||||
|
||||
it "declares a dynamic attribute" do
|
||||
attribute_value = -> { "dynamic attribute" }
|
||||
proxy.attribute_name(&attribute_value)
|
||||
expect(subject).to have_dynamic_declaration(:attribute_name).with_value(attribute_value)
|
||||
end
|
||||
|
||||
it "calls super" do
|
||||
invalid_call = -> { proxy.static_attributes_are_gone true }
|
||||
expect(invalid_call).to raise_error(NoMethodError)
|
||||
end
|
||||
end
|
||||
|
||||
describe FactoryBot::DefinitionProxy, "#sequence" do
|
||||
|
@ -209,7 +188,7 @@ describe FactoryBot::DefinitionProxy, "#trait" do
|
|||
let(:proxy) { FactoryBot::DefinitionProxy.new(subject) }
|
||||
|
||||
it "declares a trait" do
|
||||
male_trait = Proc.new { gender("Male") }
|
||||
male_trait = Proc.new { gender { "Male" } }
|
||||
proxy.trait(:male, &male_trait)
|
||||
expect(subject).to have_trait(:male).with_block(male_trait)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue