Closes#1391
Along the same lines as #1286, this commit raises a more helpful error
if somebody passes an implicit declaration as an association argument.
Before this commit, an association with an implicit trait passed as an
override attribute:
```rb
person factory: :user, invalid_attribute: implicit_trait
```
Would raise an error `KeyError: Trait not registered: "implicit_trait"`.
This is potentially confusing, since the author probably didn't intend
to define an implicit trait.
After this commit, this will raise a more helpful error:
```
ArgumentError: Association 'person' received an invalid attribute override.
Did you mean? 'invalid_attribute}: :implicit_trait}'
```
This commit separates the factory name from the overrides on initialize,
then uses attr_readers throughout instead of manipulating instance
variables.
This is a bit cleaner, and will make it easier to reused the
factory_name and overrides in a future code change.
If a new trait is defined with attributes, check if attributes matches the name of
the trait. Raise an error if so, to avoid a recursive call to the
trait.
Co-authored-by: Daniel Colsen <daniel.j.colson@thoughtbot.com>
Co-authored-by: Paras Sanghavi <sanghaviparas@gmail.com>
Why:
Another run of internal methods that should not be publicly
available from the base namespace.
This time the sequence involving methods were moved. It's worth noticing
that the ```Internal``` class is getting crowded. Maybe we can start
name-spacing the internal groups into modules under
```internal/sequence.rb``` ```internal/trait.rb``` and so on. Thoughts?
This PR:
- Moves the ```register_sequence```, ```sequence_by_name```,
```sequences``` and ```rewind_sequences``` to the
```FactoryBot::Internal``` module.
- Deprecates uses of ```sequence_by_name```, and ```sequences```
from the ```FactoryBot``` module.
- Refactor rewind sequences test to use spies
This is one of the steps towards fixing [this
issue](https://github.com/thoughtbot/factory_bot/pull/1285#1281)
Closes https://github.com/thoughtbot/factory_bot/issues/1227
In this code we are passing an implicit declaration `user`, rather than
the symbol `:user`:
```rb
factory :post do
author factory: user
end
```
In https://github.com/thoughtbot/factory_bot/issues/1227 we improved the
error message from:
```rb
undefined method 'name' for :post:Symbol
```
to:
```rb
Trait not registered: user
```
But this still doesn't make it totally obvious what the error was.
Why are we trying to register a user trait?
With this PR we update the error message to:
```rb
Association 'author' received an invalid factory argument.
Did you mean? 'factory: :user'
```
Closes#970
In this code we are passing an implicit declaration `user`, rather than
the symbol `:user`:
```rb
factory :post do
author factory: user
end
```
This will raise a confusing error:
`undefined method 'name' for :comment:Symbol`.
This is coming from the implicit declaration `#==` method,
called on lib/factory_bot/attribute_list.rb#56.
The method wasn't ever designed to compare against
objects of different classes. I added some tests for all the declaration
classes to handle being compared against other kinds of objects.
Co-authored-by: Dusan Orlovic <duleorlovic@gmail.com>
* Alphabetize gem listing in various Gemfiles [Rubocop Bundler/OrderedGems]
* Fix alignment of if/else/end statement [Rubocop Layout/ElseAlignment]
* Method definitions should have a empty line between them [Rubocop Layout/EmptyLineBetweenDefs]
* Modules, Classes, and blocks should have an empty line around them [Rubocop]
Cops:
Layout/EmptyLinesAroundBlockBody
Layout/EmptyLinesAroundModuleBody
Layout/EmptyLinesAroundClassBody
Layout/EmptyLinesAroundAccessModifier
* Keep a blank line before and after access modifiers [Rubocop Layout/EmptyLinesAroundAccessModifier]
* Remove misc extra whitespace [Rubocop Layout/ExtraSpacing]
* Indent the first line of the right-hand-side of a multi-line assignment [Rubocop Layout/IndentAssignment]
* Remove extraneous whitespace [Rubocop]
Cops:
Layout/IndentationWidth
Layout/LeadingCommentSpace
Layout/SpaceAroundEqualsInParameterDefault
Layout/SpaceInsideArrayLiteralBrackets
Layout/SpaceInsideBlockBraces
Layout/SpaceInsideParens
Layout/TrailingBlankLines
* Revert rubocop changes to gemfiles; exclude files from rubocop checks
The files in gemfiles/ are generated by Appraisal, so we shouldn't edit them. Instead, let's tell RuboCop to exclude this directory.