1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Improve error for static attribute attempts

Some people upgrade straight from factory_bot < 4.11 to factory_bot >=
5.0 and miss the deprecation cycle for static attributes. I have gotten
some feedback that the NoMethodError is confusing. Since we know that in
most cases people are seeing the NoMethodError when trying to define
static attributes, we offer them a "Did you mean?"-style suggestion for
how they might update to dynamic attributes.

I removed the extra test for setter methods. It was a remnant of
something I had removed in #1200. I see no reason to have special
treatment for setters at this point.

Closes https://github.com/thoughtbot/factory_bot/issues/1272
This commit is contained in:
Daniel Colson 2019-04-23 22:45:55 -04:00
parent 041216d250
commit f898371f8f
No known key found for this signature in database
GPG key ID: 88A364BBE77B1353
2 changed files with 7 additions and 18 deletions

View file

@ -94,9 +94,10 @@ module FactoryBot
elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory)
association(name, *args)
else
raise NoMethodError.new(
"undefined method '#{name}' in '#{@definition.name}' factory",
)
raise NoMethodError.new(<<~MSG)
undefined method '#{name}' in '#{@definition.name}' factory
Did you mean? '#{name} { #{args.first.inspect} }'
MSG
end
end

View file

@ -72,23 +72,11 @@ describe FactoryBot::DefinitionProxy, "#method_missing" do
definition = FactoryBot::Definition.new(:broken)
proxy = FactoryBot::DefinitionProxy.new(definition)
invalid_call = -> { proxy.static_attributes_are_gone true }
invalid_call = -> { proxy.static_attributes_are_gone "true" }
expect(invalid_call).to raise_error(
NoMethodError,
"undefined method 'static_attributes_are_gone' in 'broken' factory",
)
end
end
context "when called with a setter method" do
it "raises a NoMethodError" do
definition = FactoryBot::Definition.new(:broken)
proxy = FactoryBot::DefinitionProxy.new(definition)
invalid_call = -> { proxy.setter_method = true }
expect(invalid_call).to raise_error(
NoMethodError,
"undefined method 'setter_method=' in 'broken' factory",
"undefined method 'static_attributes_are_gone' in 'broken' factory\n" \
"Did you mean? 'static_attributes_are_gone { \"true\" }'\n",
)
end
end