From 6afa639804cf7fd7e17d0792b475806b90ab9bc7 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Sun, 25 Nov 2018 22:16:22 -0500 Subject: [PATCH] Improve documentation around implicit attributes [skip ci] Closes #1017 --- GETTING_STARTED.md | 8 +++++++- lib/factory_bot/definition_proxy.rb | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index a0d47dd..d3e9a73 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -541,6 +541,9 @@ factory :user do end ``` +Note that defining sequences as implicit attributes will not work if you have a +factory with the same name as the sequence. + And it's also possible to define an in-line sequence that is only used in a particular factory: @@ -649,7 +652,7 @@ factory :story do end ``` -Traits can be used as attributes: +Traits can be used as implicit attributes: ```ruby factory :week_long_published_story_with_title, parent: :story do @@ -659,6 +662,9 @@ factory :week_long_published_story_with_title, parent: :story do end ``` +Note that defining traits as implicit attributes will not work if you have a +factory or sequence with the same name as the trait. + Traits that define the same attributes won't raise AttributeDefinitionErrors; the trait that defines the attribute latest gets precedence. diff --git a/lib/factory_bot/definition_proxy.rb b/lib/factory_bot/definition_proxy.rb index 84be01a..b762556 100644 --- a/lib/factory_bot/definition_proxy.rb +++ b/lib/factory_bot/definition_proxy.rb @@ -56,17 +56,20 @@ module FactoryBot # # are equivalent. # - # If no argument or block is given, factory_bot will look for a sequence - # or association with the same name. This means that: + # If no argument or block is given, factory_bot will first look for an + # association, then for a sequence, and finally for a trait with the same + # name. This means that given an "admin" trait, an "email" sequence, and an + # "account" factory: # - # factory :user do - # email { create(:email) } + # factory :user, traits: [:admin] do + # email { generate(:email) } # association :account # end # # and: # # factory :user do + # admin # email # account # end