mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
1d6170af2b
This has come up a few times, and I can see why it might be helpful to
have access to full backtraces when debugging a factory error uncovered
by `FactoryBot.lint`. But since most of the time I don't want the extra
noise from the backtrace, I added this as a verbose option.
The default message is still:
```
The following factories are invalid:
* user - undefined method `save!' for #<User:0x00007ff0cbc89100>
* admin - undefined method `save!' for #<User:0x00007ff0cbc73e40>
```
And with the verbose option (usually with more lines of backtrace):
```
The following factories are invalid:
* user - undefined method `save!' for #<User:0x00007ff0cbc89100>
/Users/.../thoughtbot/factory_bot/lib/factory_bot/evaluation.rb:18:in `create'
/Users/.../factory_bot/lib/factory_bot/strategy/create.rb:12:in `block in result'
* admin - undefined method `save!' for #<User:0x00007ff0cbc73e40>
/Users/.../thoughtbot/factory_bot/lib/factory_bot/evaluation.rb:18:in `create'
/Users/.../factory_bot/lib/factory_bot/strategy/create.rb:12:in `block in result'
```
I moved the linting option defaults out of the FactoryBot.lint method
and into keyword argument defaults in Linter#initialize. This seems a
bit cleaner, and now we will get an error if we pass an option we don't
understand (before 6e511597
we had a test that passed in a bogus
option)
Closes #710
Closes #1124
I am opening a new PR since the original PR is years old and it seemed
unkind to request changes after so long. Instead I will list the authors
as co-authors.
Co-authored-by: Jack Kinsella <jack.kinsella@gmail.com>
Co-authored-by: Jasper Woudenberg <mail@jasperwoudenberg.com>
114 lines
2.6 KiB
Ruby
114 lines
2.6 KiB
Ruby
module FactoryBot
|
|
class Linter
|
|
def initialize(factories, strategy: :create, traits: false, verbose: false)
|
|
@factories_to_lint = factories
|
|
@factory_strategy = strategy
|
|
@traits = traits
|
|
@verbose = verbose
|
|
@invalid_factories = calculate_invalid_factories
|
|
end
|
|
|
|
def lint!
|
|
if invalid_factories.any?
|
|
raise InvalidFactoryError, error_message
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :factories_to_lint, :invalid_factories, :factory_strategy
|
|
|
|
def calculate_invalid_factories
|
|
factories_to_lint.reduce(Hash.new([])) do |result, factory|
|
|
errors = lint(factory)
|
|
result[factory] |= errors unless errors.empty?
|
|
result
|
|
end
|
|
end
|
|
|
|
class FactoryError
|
|
def initialize(wrapped_error, factory)
|
|
@wrapped_error = wrapped_error
|
|
@factory = factory
|
|
end
|
|
|
|
def message
|
|
message = @wrapped_error.message
|
|
"* #{location} - #{message} (#{@wrapped_error.class.name})"
|
|
end
|
|
|
|
def verbose_message
|
|
<<~MESSAGE
|
|
#{message}
|
|
#{@wrapped_error.backtrace.join("\n ")}
|
|
MESSAGE
|
|
end
|
|
|
|
def location
|
|
@factory.name
|
|
end
|
|
end
|
|
|
|
class FactoryTraitError < FactoryError
|
|
def initialize(wrapped_error, factory, trait_name)
|
|
super(wrapped_error, factory)
|
|
@trait_name = trait_name
|
|
end
|
|
|
|
def location
|
|
"#{@factory.name}+#{@trait_name}"
|
|
end
|
|
end
|
|
|
|
def lint(factory)
|
|
if @traits
|
|
lint_factory(factory) + lint_traits(factory)
|
|
else
|
|
lint_factory(factory)
|
|
end
|
|
end
|
|
|
|
def lint_factory(factory)
|
|
result = []
|
|
begin
|
|
FactoryBot.public_send(factory_strategy, factory.name)
|
|
rescue StandardError => error
|
|
result |= [FactoryError.new(error, factory)]
|
|
end
|
|
result
|
|
end
|
|
|
|
def lint_traits(factory)
|
|
result = []
|
|
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
|
begin
|
|
FactoryBot.public_send(factory_strategy, factory.name, trait_name)
|
|
rescue StandardError => error
|
|
result |=
|
|
[FactoryTraitError.new(error, factory, trait_name)]
|
|
end
|
|
end
|
|
result
|
|
end
|
|
|
|
def error_message
|
|
lines = invalid_factories.map do |_factory, exceptions|
|
|
exceptions.map(&error_message_type)
|
|
end.flatten
|
|
|
|
<<~ERROR_MESSAGE.strip
|
|
The following factories are invalid:
|
|
|
|
#{lines.join("\n")}
|
|
ERROR_MESSAGE
|
|
end
|
|
|
|
def error_message_type
|
|
if @verbose
|
|
:verbose_message
|
|
else
|
|
:message
|
|
end
|
|
end
|
|
end
|
|
end
|