mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Refactor attribute_list_spec.rb to conform to Let's Not style (#1341)
* Refactor attribute_list_spec.rb to conform to Let's Not style * PR feedback * Re-name 'list' method to 'build_attribute_list', and extract to top-level scope so it can be re-used * Removed some unnecessary attribute declarations * Wrap 'build_attribute_list' method inside a module, so it's not defined on the global Object class
This commit is contained in:
parent
de024de9b2
commit
14193292cc
1 changed files with 85 additions and 57 deletions
|
@ -1,26 +1,37 @@
|
|||
module AttributeList
|
||||
def build_attribute_list(*attributes)
|
||||
FactoryBot::AttributeList.new.tap do |list|
|
||||
attributes.each { |attribute| list.define_attribute(attribute) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe FactoryBot::AttributeList, "#define_attribute" do
|
||||
it "maintains a list of attributes" do
|
||||
attribute = double(:attribute, name: :attribute_name)
|
||||
another_attribute = double(:attribute, name: :another_attribute_name)
|
||||
list = FactoryBot::AttributeList.new
|
||||
|
||||
subject.define_attribute(attribute)
|
||||
expect(subject.to_a).to eq [attribute]
|
||||
list.define_attribute(attribute)
|
||||
expect(list.to_a).to eq [attribute]
|
||||
|
||||
subject.define_attribute(another_attribute)
|
||||
expect(subject.to_a).to eq [attribute, another_attribute]
|
||||
list.define_attribute(another_attribute)
|
||||
expect(list.to_a).to eq [attribute, another_attribute]
|
||||
end
|
||||
|
||||
it "returns the attribute" do
|
||||
attribute = double(:attribute, name: :attribute_name)
|
||||
list = FactoryBot::AttributeList.new
|
||||
|
||||
expect(subject.define_attribute(attribute)).to eq attribute
|
||||
expect(list.define_attribute(attribute)).to eq attribute
|
||||
end
|
||||
|
||||
it "raises if an attribute has already been defined" do
|
||||
attribute = double(:attribute, name: :attribute_name)
|
||||
list = FactoryBot::AttributeList.new
|
||||
|
||||
expect do
|
||||
2.times { subject.define_attribute(attribute) }
|
||||
2.times { list.define_attribute(attribute) }
|
||||
end.to raise_error(
|
||||
FactoryBot::AttributeDefinitionError,
|
||||
"Attribute already defined: attribute_name",
|
||||
|
@ -29,14 +40,12 @@ describe FactoryBot::AttributeList, "#define_attribute" do
|
|||
end
|
||||
|
||||
describe FactoryBot::AttributeList, "#define_attribute with a named attribute list" do
|
||||
subject { FactoryBot::AttributeList.new(:author) }
|
||||
|
||||
let(:association_with_same_name) { FactoryBot::Attribute::Association.new(:author, :author, {}) }
|
||||
let(:association_with_different_name) { FactoryBot::Attribute::Association.new(:author, :post, {}) }
|
||||
|
||||
it "raises when the attribute is a self-referencing association" do
|
||||
association_with_same_name = FactoryBot::Attribute::Association.new(:author, :author, {})
|
||||
list = FactoryBot::AttributeList.new(:author)
|
||||
|
||||
expect do
|
||||
subject.define_attribute(association_with_same_name)
|
||||
list.define_attribute(association_with_same_name)
|
||||
end.to raise_error(
|
||||
FactoryBot::AssociationDefinitionError,
|
||||
"Self-referencing association 'author' in 'author'",
|
||||
|
@ -44,47 +53,47 @@ describe FactoryBot::AttributeList, "#define_attribute with a named attribute li
|
|||
end
|
||||
|
||||
it "does not raise when the attribute is not a self-referencing association" do
|
||||
expect { subject.define_attribute(association_with_different_name) }.to_not raise_error
|
||||
association_with_different_name = FactoryBot::Attribute::Association.new(:author, :post, {})
|
||||
list = FactoryBot::AttributeList.new
|
||||
|
||||
expect { list.define_attribute(association_with_different_name) }.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe FactoryBot::AttributeList, "#apply_attributes" do
|
||||
def list(*attributes)
|
||||
FactoryBot::AttributeList.new.tap do |list|
|
||||
attributes.each { |attribute| list.define_attribute(attribute) }
|
||||
end
|
||||
end
|
||||
include AttributeList
|
||||
|
||||
it "adds attributes in the order defined" do
|
||||
attribute1 = double(:attribute1, name: :attribute1)
|
||||
attribute2 = double(:attribute2, name: :attribute2)
|
||||
attribute3 = double(:attribute3, name: :attribute3)
|
||||
list = FactoryBot::AttributeList.new
|
||||
|
||||
subject.define_attribute(attribute1)
|
||||
subject.apply_attributes(list(attribute2, attribute3))
|
||||
expect(subject.to_a).to eq [attribute1, attribute2, attribute3]
|
||||
list.define_attribute(attribute1)
|
||||
list.apply_attributes(build_attribute_list(attribute2))
|
||||
expect(list.to_a).to eq [attribute1, attribute2]
|
||||
end
|
||||
end
|
||||
|
||||
describe FactoryBot::AttributeList, "#associations" do
|
||||
let(:email_attribute) do
|
||||
FactoryBot::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" })
|
||||
end
|
||||
let(:author_attribute) { FactoryBot::Attribute::Association.new(:author, :user, {}) }
|
||||
let(:profile_attribute) { FactoryBot::Attribute::Association.new(:profile, :profile, {}) }
|
||||
|
||||
before do
|
||||
subject.define_attribute(email_attribute)
|
||||
subject.define_attribute(author_attribute)
|
||||
subject.define_attribute(profile_attribute)
|
||||
end
|
||||
include AttributeList
|
||||
|
||||
it "returns associations" do
|
||||
expect(subject.associations.to_a).to eq [author_attribute, profile_attribute]
|
||||
email_attribute = FactoryBot::Attribute::Dynamic.new(
|
||||
:email,
|
||||
false,
|
||||
->(u) { "#{u.full_name}@example.com" },
|
||||
)
|
||||
author_attribute = FactoryBot::Attribute::Association.new(:author, :user, {})
|
||||
profile_attribute = FactoryBot::Attribute::Association.new(:profile, :profile, {})
|
||||
list = build_attribute_list(email_attribute, author_attribute, profile_attribute)
|
||||
|
||||
expect(list.associations.to_a).to eq [author_attribute, profile_attribute]
|
||||
end
|
||||
end
|
||||
|
||||
describe FactoryBot::AttributeList, "filter based on ignored attributes" do
|
||||
include AttributeList
|
||||
|
||||
def build_ignored_attribute(name)
|
||||
FactoryBot::Attribute::Dynamic.new(name, true, -> { "value" })
|
||||
end
|
||||
|
@ -93,24 +102,28 @@ describe FactoryBot::AttributeList, "filter based on ignored attributes" do
|
|||
FactoryBot::Attribute::Dynamic.new(name, false, -> { "value" })
|
||||
end
|
||||
|
||||
before do
|
||||
subject.define_attribute(build_ignored_attribute(:comments_count))
|
||||
subject.define_attribute(build_ignored_attribute(:posts_count))
|
||||
subject.define_attribute(build_non_ignored_attribute(:email))
|
||||
subject.define_attribute(build_non_ignored_attribute(:first_name))
|
||||
subject.define_attribute(build_non_ignored_attribute(:last_name))
|
||||
end
|
||||
|
||||
it "filters #ignored attributes" do
|
||||
expect(subject.ignored.map(&:name)).to eq [:comments_count, :posts_count]
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:comments_count),
|
||||
build_non_ignored_attribute(:email),
|
||||
)
|
||||
|
||||
expect(list.ignored.names).to eq [:comments_count]
|
||||
end
|
||||
|
||||
it "filters #non_ignored attributes" do
|
||||
expect(subject.non_ignored.map(&:name)).to eq [:email, :first_name, :last_name]
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:comments_count),
|
||||
build_non_ignored_attribute(:email),
|
||||
)
|
||||
|
||||
expect(list.non_ignored.names).to eq [:email]
|
||||
end
|
||||
end
|
||||
|
||||
describe FactoryBot::AttributeList, "generating names" do
|
||||
include AttributeList
|
||||
|
||||
def build_ignored_attribute(name)
|
||||
FactoryBot::Attribute::Dynamic.new(name, true, -> { "value" })
|
||||
end
|
||||
|
@ -123,28 +136,43 @@ describe FactoryBot::AttributeList, "generating names" do
|
|||
FactoryBot::Attribute::Association.new(name, :user, {})
|
||||
end
|
||||
|
||||
before do
|
||||
subject.define_attribute(build_ignored_attribute(:comments_count))
|
||||
subject.define_attribute(build_ignored_attribute(:posts_count))
|
||||
subject.define_attribute(build_non_ignored_attribute(:email))
|
||||
subject.define_attribute(build_non_ignored_attribute(:first_name))
|
||||
subject.define_attribute(build_non_ignored_attribute(:last_name))
|
||||
subject.define_attribute(build_association(:avatar))
|
||||
end
|
||||
|
||||
it "knows all its #names" do
|
||||
expect(subject.names).to eq [:comments_count, :posts_count, :email, :first_name, :last_name, :avatar]
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:comments_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
)
|
||||
|
||||
expect(list.names).to eq [:comments_count, :last_name, :avatar]
|
||||
end
|
||||
|
||||
it "knows all its #names for #ignored attributes" do
|
||||
expect(subject.ignored.names).to eq [:comments_count, :posts_count]
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:posts_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
)
|
||||
|
||||
expect(list.ignored.names).to eq [:posts_count]
|
||||
end
|
||||
|
||||
it "knows all its #names for #non_ignored attributes" do
|
||||
expect(subject.non_ignored.names).to eq [:email, :first_name, :last_name, :avatar]
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:posts_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
)
|
||||
|
||||
expect(list.non_ignored.names).to eq [:last_name, :avatar]
|
||||
end
|
||||
|
||||
it "knows all its #names for #associations" do
|
||||
expect(subject.associations.names).to eq [:avatar]
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:posts_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
)
|
||||
|
||||
expect(list.associations.names).to eq [:avatar]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue