1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/acceptance/associations_spec.rb
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

19 lines
528 B
Ruby

describe "associations" do
context "when accidentally using an implicit delcaration for the factory" do
it "raises an error about the trait not being registered" do
define_class("Post")
FactoryBot.define do
factory :post do
author factory: user
end
end
expect { FactoryBot.build(:post) }.to raise_error(
ArgumentError,
"Association 'author' received an invalid factory argument.\n" \
"Did you mean? 'factory: :user'\n",
)
end
end
end