Autocorrect all static attributes to dynamic

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.
This commit is contained in:
Daniel Colson 2018-07-29 11:30:02 -04:00
parent b4e1303904
commit bf04aaa068
27 changed files with 109 additions and 109 deletions

View File

@ -4,7 +4,7 @@ Feature: FactoryBot can find factory definitions correctly
"""
FactoryBot.define do
factory :awesome_category, :class => Category do
name "awesome!!!"
name { "awesome!!!" }
end
end
"""
@ -19,7 +19,7 @@ Feature: FactoryBot can find factory definitions correctly
"""
FactoryBot.define do
factory :another_awesome_category, :class => Category do
name "awesome!!!"
name { "awesome!!!" }
end
end
"""
@ -34,7 +34,7 @@ Feature: FactoryBot can find factory definitions correctly
"""
FactoryBot.define do
factory :great_category, :class => Category do
name "great!!!"
name { "great!!!" }
end
end
"""
@ -50,11 +50,11 @@ Feature: FactoryBot can find factory definitions correctly
FactoryBot.define do
sequence(:great)
trait :admin do
admin true
admin { true }
end
factory :handy_category, :class => Category do
name "handy"
name { "handy" }
end
end
"""
@ -64,7 +64,7 @@ Feature: FactoryBot can find factory definitions correctly
FactoryBot.modify do
factory :handy_category do
name "HANDY!!!"
name { "HANDY!!!" }
end
end
"""

View File

@ -18,7 +18,7 @@ describe "using ActiveSupport::Instrumentation to track factory interaction" do
define_model("User", email: :string)
FactoryBot.define do
factory :user do
email "john@example.com"
email { "john@example.com" }
factory :slow_user do
after(:build) { Kernel.sleep(0.1) }

View File

@ -6,7 +6,7 @@ describe "aliases and overrides" do
FactoryBot.define do
factory :user do
two "set value"
two { "set value" }
end
end
end

View File

@ -9,7 +9,7 @@ describe "attribute aliases" do
FactoryBot.define do
factory :user do
factory :user_with_name do
name "John Doe"
name { "John Doe" }
end
end

View File

@ -4,9 +4,9 @@ describe "declaring attributes on a Factory that are private methods on Object"
FactoryBot.define do
factory :website do
system false
link "http://example.com"
sleep 15
system { false }
link { "http://example.com" }
sleep { 15 }
end
end
end

View File

@ -85,7 +85,7 @@ describe "`attributes_for` for a class whose constructor has required params" do
FactoryBot.define do
factory :user do
name "John Doe"
name { "John Doe" }
end
end
end

View File

@ -14,7 +14,7 @@ describe "a generated attributes hash where order matters" do
evaluates_third { evaluates_second }
factory :child_model do
static 1
static { 1 }
end
end
@ -22,7 +22,7 @@ describe "a generated attributes hash where order matters" do
evaluates_first { static }
evaluates_second { evaluates_first }
evaluates_third { evaluates_second }
static 1
static { 1 }
end
end
end

View File

@ -4,7 +4,7 @@ describe "build multiple instances" do
FactoryBot.define do
factory(:post) do |post|
post.title "Through the Looking Glass"
post.title { "Through the Looking Glass" }
post.position { rand(10**4) }
end
end

View File

@ -144,8 +144,8 @@ describe "custom callbacks" do
FactoryBot.define do
factory :user do
first_name "John"
last_name "Doe"
first_name { "John" }
last_name { "Doe" }
before(:custom) { |instance| instance.first_name = "Overridden First" }
after(:custom) { |instance| instance.last_name = "Overridden Last" }

View File

@ -4,7 +4,7 @@ describe "create multiple instances" do
FactoryBot.define do
factory(:post) do |post|
post.title "Through the Looking Glass"
post.title { "Through the Looking Glass" }
post.position { rand(10**4) }
end
end
@ -73,16 +73,16 @@ describe "multiple creates and transient attributes to dynamically build attribu
FactoryBot.define do
factory :post do
title "Through the Looking Glass"
title { "Through the Looking Glass" }
user
end
factory :user do
name "John Doe"
name { "John Doe" }
factory :user_with_posts do
transient do
posts_count 5
posts_count { 5 }
end
after(:create) do |user, evaluator|

View File

@ -4,7 +4,7 @@ describe "create multiple instances" do
FactoryBot.define do
factory(:post) do |post|
post.title "Through the Looking Glass"
post.title { "Through the Looking Glass" }
post.position { rand(10**4) }
end
end

View File

@ -94,7 +94,7 @@ describe "a custom create passing in an evaluator" do
FactoryBot.define do
factory :user do
transient { creation_name "evaluator" }
transient { creation_name { "evaluator" } }
to_create do |user, evaluator|
user.name = evaluator.creation_name

View File

@ -4,11 +4,11 @@ describe "defining a child factory before a parent" do
FactoryBot.define do
factory :admin, parent: :user do
admin true
admin { true }
end
factory :user do
name "awesome"
name { "awesome" }
end
end
end

View File

@ -23,7 +23,7 @@ describe "attributes defined using Symbol#to_proc" do
FactoryBot.define do
factory :user do
password "foo"
password { "foo" }
password_confirmation &:password
end
end

View File

@ -11,7 +11,7 @@ describe 'global to_create' do
end
factory :user do
name 'John Doe'
name { 'John Doe' }
factory :child_user
@ -21,7 +21,7 @@ describe 'global to_create' do
end
factory :post do
name 'Great title'
name { 'Great title' }
factory :child_post
@ -77,7 +77,7 @@ describe 'global skip_create' do
end
factory :user do
name 'John Doe'
name { 'John Doe' }
factory :child_user
@ -87,7 +87,7 @@ describe 'global skip_create' do
end
factory :post do
name 'Great title'
name { 'Great title' }
factory :child_post

View File

@ -63,7 +63,7 @@ describe "initialize_with non-ORM-backed objects" do
factory :report_generator do
transient do
name "My Awesome Report"
name { "My Awesome Report" }
end
initialize_with { ReportGenerator.new(name, FactoryBot.generate(:random_data)) }
@ -93,14 +93,14 @@ describe "initialize_with parent and child factories" do
FactoryBot.define do
factory :awesome do
transient do
name "Great"
name { "Great" }
end
initialize_with { Awesome.new(name) }
factory :sub_awesome do
transient do
name "Sub"
name { "Sub" }
end
end
@ -133,7 +133,7 @@ describe "initialize_with implicit constructor" do
FactoryBot.define do
factory :awesome do
transient do
name "Great"
name { "Great" }
end
initialize_with { new(name) }
@ -194,7 +194,7 @@ describe "initialize_with has access to all attributes for construction" do
factory :user do
transient do
ignored "of course!"
ignored { "of course!" }
end
email

View File

@ -6,7 +6,7 @@ describe "finding factories keyed by class instead of symbol" do
FactoryBot.define do
factory :user do
name "John Doe"
name { "John Doe" }
sequence(:email) { |n| "person#{n}@example.com" }
end
end

View File

@ -33,7 +33,7 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name 'assigned'
name { 'assigned' }
end
end
@ -68,7 +68,7 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "assigned"
name { "assigned" }
end
end
@ -82,7 +82,7 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "assigned"
name { "assigned" }
end
end
@ -98,9 +98,9 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "Yep"
name { "Yep" }
trait :unnamed do
name nil
name { nil }
end
end
end
@ -123,9 +123,9 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "Yep"
name { "Yep" }
trait :renamed do
name "Yessir"
name { "Yessir" }
end
end
end
@ -144,9 +144,9 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "Yep"
name { "Yep" }
trait :unnamed do
name nil
name { nil }
end
end
end
@ -171,7 +171,7 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "Barbara"
name { "Barbara" }
end
end
@ -191,10 +191,10 @@ The following factories are invalid:
FactoryBot.define do
factory :user do
name "Barbara"
name { "Barbara" }
trait :male do
name "Bob"
name { "Bob" }
end
end
end

View File

@ -15,7 +15,7 @@ describe "modifying factories" do
end
factory :admin do
admin true
admin { true }
end
end
end
@ -25,7 +25,7 @@ describe "modifying factories" do
before do
FactoryBot.modify do
factory :user do
name "Great User"
name { "Great User" }
end
end
end
@ -43,7 +43,7 @@ describe "modifying factories" do
it "does allow the factory to be subsequently modified" do
FactoryBot.modify do
factory :user do
name "Overridden again!"
name { "Overridden again!" }
end
end
@ -55,7 +55,7 @@ describe "modifying factories" do
before do
FactoryBot.modify do
factory :user do
name "Great User"
name { "Great User" }
after(:create) do |user|
user.name = user.name.downcase
user.login = nil
@ -74,7 +74,7 @@ describe "modifying factories" do
before do
FactoryBot.define do
trait :rockstar do
name "Johnny Rockstar!!!"
name { "Johnny Rockstar!!!" }
end
end
@ -98,7 +98,7 @@ describe "modifying factories" do
FactoryBot.modify do
factory :user do
email { "#{name}-modified@example.com" }
name "Great User"
name { "Great User" }
end
end
end
@ -156,7 +156,7 @@ describe "modifying factories" do
it "doesn't overwrite already defined child's attributes" do
FactoryBot.modify do
factory :user do
admin false
admin { false }
end
end
expect(create(:admin)).to be_admin
@ -165,7 +165,7 @@ describe "modifying factories" do
it "allows for overriding child classes" do
FactoryBot.modify do
factory :admin do
admin false
admin { false }
end
end

View File

@ -3,24 +3,24 @@ describe "modifying inherited factories with traits" do
define_model('User', gender: :string, admin: :boolean, age: :integer)
FactoryBot.define do
factory :user do
trait(:female) { gender "Female" }
trait(:male) { gender "Male" }
trait(:female) { gender { "Female" } }
trait(:male) { gender { "Male" } }
trait(:young_admin) do
admin true
age 17
admin { true }
age { 17 }
end
female
young_admin
factory :female_user do
gender "Female"
age 25
gender { "Female" }
age { 25 }
end
factory :male_user do
gender "Male"
gender { "Male" }
end
end
end
@ -39,7 +39,7 @@ describe "modifying inherited factories with traits" do
it "allows modification of attributes created via traits" do
FactoryBot.modify do
factory :male_user do
age 20
age { 20 }
end
end

View File

@ -15,13 +15,13 @@ describe "attribute overrides" do
FactoryBot.define do
factory :user do
factory :admin do
admin true
admin { true }
end
end
factory :post do
user
title "default title"
title { "default title" }
end
end
end

View File

@ -4,13 +4,13 @@ describe "an instance generated by a factory that inherits from another factory"
FactoryBot.define do
factory :user do
name "John"
name { "John" }
email { "#{name.downcase}@example.com" }
login { email }
factory :admin do
name "admin"
admin true
name { "admin" }
admin { true }
upper_email { email.upcase }
end
@ -19,7 +19,7 @@ describe "an instance generated by a factory that inherits from another factory"
end
factory :no_email do
email ""
email { "" }
end
factory :bill do
@ -69,10 +69,10 @@ describe "nested factories with different parents" do
FactoryBot.define do
factory :user do
name "Basic User"
name { "Basic User" }
factory :male_user do
name "John Doe"
name { "John Doe" }
end
factory :uppercase_male_user, parent: :male_user do

View File

@ -7,7 +7,7 @@ describe 'setting private attributes' do
FactoryBot.define do
factory :user do
foo 123
foo { 123 }
end
end

View File

@ -22,7 +22,7 @@ describe "register custom strategies" do
before do
FactoryBot.define do
factory :named_object do
name "Great"
name { "Great" }
end
end
end
@ -65,7 +65,7 @@ describe "including FactoryBot::Syntax::Methods when custom strategies have been
before do
FactoryBot.define do
factory :named_object do
name "Great"
name { "Great" }
end
end
end
@ -93,7 +93,7 @@ describe "associations without overriding :strategy" do
end
factory :user do
name "John Doe"
name { "John Doe" }
end
end
end
@ -137,7 +137,7 @@ describe "associations overriding :strategy" do
end
factory :user do
name "John Doe"
name { "John Doe" }
end
end
end

View File

@ -6,7 +6,7 @@ describe "skipping the default create" do
factory :user do
skip_create
email "john@example.com"
email { "john@example.com" }
end
end
end

View File

@ -14,28 +14,28 @@ describe "an instance generated by a factory with multiple traits" do
end
factory :user do
name "John"
name { "John" }
trait :great do
great "GREAT!!!"
great { "GREAT!!!" }
end
trait :admin do
admin true
admin { true }
end
trait :admin_trait do
admin true
admin { true }
end
trait :male do
name "Joe"
gender "Male"
name { "Joe" }
gender { "Male" }
end
trait :female do
name "Jane"
gender "Female"
name { "Jane" }
gender { "Female" }
end
factory :great_user do
@ -54,8 +54,8 @@ describe "an instance generated by a factory with multiple traits" do
factory :female, traits: [:female] do
trait :admin do
admin true
name "Judy"
admin { true }
name { "Judy" }
end
factory :female_great_user do
@ -75,7 +75,7 @@ describe "an instance generated by a factory with multiple traits" do
end
factory :user_with_email, class: User, traits: [:email] do
name "Bill"
name { "Bill" }
end
end
end
@ -182,7 +182,7 @@ describe "traits with callbacks" do
FactoryBot.define do
factory :user do
name "John"
name { "John" }
trait :great do
after(:create) { |user| user.name.upcase! }
@ -223,10 +223,10 @@ describe "traits added via strategy" do
FactoryBot.define do
factory :user do
name "John"
name { "John" }
trait :admin do
admin true
admin { true }
end
trait :great do
@ -307,7 +307,7 @@ describe "traits and dynamic attributes that are applied simultaneously" do
end
factory :user do
name "John"
name { "John" }
email
combined { "#{name} <#{email}>" }
end
@ -352,14 +352,14 @@ describe "inline traits overriding existing attributes" do
FactoryBot.define do
factory :user do
status "pending"
status { "pending" }
trait(:accepted) { status "accepted" }
trait(:declined) { status "declined" }
trait(:accepted) { status { "accepted" } }
trait(:declined) { status { "declined" } }
factory :declined_user, traits: [:declined]
factory :extended_declined_user, traits: [:declined] do
status "extended_declined"
status { "extended_declined" }
end
end
end
@ -400,8 +400,8 @@ describe "making sure the factory is properly compiled the first time we want to
FactoryBot.define do
factory :user do
trait(:female) { gender "female" }
trait(:admin) { role "admin" }
trait(:female) { gender { "female" } }
trait(:admin) { role { "admin" } }
factory :female_user do
female
@ -585,7 +585,7 @@ describe "nested implicit traits" do
before do
FactoryBot.define do
trait :female do
gender "female"
gender { "female" }
to_create { |instance| instance.gender = instance.gender.upcase }
end
@ -594,7 +594,7 @@ describe "nested implicit traits" do
end
trait :admin do
role "admin"
role { "admin" }
after(:build) { |instance| instance.role = instance.role.upcase }
end
@ -616,7 +616,7 @@ describe "nested implicit traits" do
FactoryBot.define do
factory :user do
trait :female do
gender "female"
gender { "female" }
to_create { |instance| instance.gender = instance.gender.upcase }
end
@ -625,7 +625,7 @@ describe "nested implicit traits" do
end
trait :admin do
role "admin"
role { "admin" }
after(:build) { |instance| instance.role = instance.role.upcase }
end
@ -648,7 +648,7 @@ describe "implicit traits containing callbacks" do
FactoryBot.define do
factory :user do
value 0
value { 0 }
trait :trait_with_callback do
after(:build) { |user| user.value += 1 }
@ -684,10 +684,10 @@ describe "traits used in associations" do
FactoryBot.define do
factory :user do
admin false
admin { false }
trait :admin do
admin true
admin { true }
end
end

View File

@ -8,8 +8,8 @@ describe "transient attributes" do
factory :user do
transient do
four { 2 + 2 }
rockstar true
upcased false
rockstar { true }
upcased { false }
end
name { "#{FactoryBot.generate(:name)}#{" - Rockstar" if rockstar}" }