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#710Closes#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>
These specs have no expectations, and they are passing
`validate_traits` as an option, which is not an option the linter
understands.
Since the first spec is already covered by "allows for selective
linting" above, and what I assume the second spec was meaning to cover
is covered by the trait specs, I see no reason to keep these around.
Most of this was fixed by adding the `attribute-defined-statically-cop`
branch of `thoughtbot/rubocop-rspec` to the Gemfile and running:
```sh
rubocop \
--require rubocop-rspec \
--only FactoryBot/AttributeDefinedStatically \
--auto-correct
```
I had to update the cucumber tests manually, and I realized our changes
don't handle `ignore` blocks or blocks with arity 1 that use the yielded
DefinitionProxy. I will update
https://github.com/rubocop-hq/rubocop-rspec/pull/666to handle these
cases.
Why?
Traits are another way to extend or modify behavior of a factory;
because of this, applied traits may result in invalid factories, even if
the base factory is valid.
To enable this functionality, use:
FactoryGirl.lint traits:true
Note that, because FactoryGirl uses the `create` strategy when linting,
this may greatly slow the time to lint factories.
This commit adds FactoryGirl.lint, which iterates over each defined
factory, builds it, and ensures it is valid. If any factories are
invalid, an exception is raised and includes the names of each invalid
factory.