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

9 commits

Author SHA1 Message Date
Daniel Colson
f606018a78 Raise more helpful error for invalid attribute
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}'
```
2020-06-22 20:01:32 -04:00
Daniel Colson
f223c7d763 Use attr_readers in association declaration
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.
2020-06-21 22:09:09 -04:00
Sweta
5ccc564923
Fix self referencing trait error (#1294)
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>
2019-09-10 16:24:20 -07:00
Alejandro Dustet
5947e10578 Deprecate and move to Internal sequence methods
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)
2019-06-04 19:02:51 -04:00
Daniel Colson
041216d250
Offer advice for association typo
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'
```
2019-04-26 11:12:05 -04:00
Daniel Colson
2b524b060d Raise more meaningful error for unregistered trait
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>
2018-10-31 12:50:39 -04:00
Colin Ross
a56b68bc9c Fix assorted RuboCop spacing volations (#1203)
* 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.
2018-09-27 21:35:05 -04:00
Daniel Colson
0473865b93 Remove static attributes
Co-authored-by: George Wambold <georgewambold@gmail.com>
2018-09-14 19:27:13 +00:00
Avielle
c716ce01b4 Replace 'girl' with 'bot' everywhere (#1051)
Also: add a deprecation warning to factory_girl, asking users to switch to
factory_bot

https://github.com/thoughtbot/factory_girl/issues/921
2017-10-20 15:20:28 -04:00