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

Refactor declaration_list_spec.rb to conform to Let's Not style (#1340)

* Refactor declaration_list_spec.rb to conform to Let's Not style

* Fix Hound error ('Naming/VariableNumber: Use normalcase for variable numbers.')
This commit is contained in:
Richie Thomas 2019-10-25 09:09:59 -07:00 committed by GitHub
parent c62ca43d17
commit 03e59b4a6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,74 +1,90 @@
describe FactoryBot::DeclarationList, "#attributes" do
let(:attribute_1) { double("attribute 1") }
let(:attribute_2) { double("attribute 2") }
let(:attribute_3) { double("attribute 3") }
let(:declaration_1) do
double(
"declaration 1",
to_attributes: [attribute_1, attribute_2],
)
end
let(:declaration_2) do
double("declaration_2", to_attributes: [attribute_3])
end
it "returns an AttributeList" do
expect(subject.attributes).to be_a(FactoryBot::AttributeList)
end
declaration_list = FactoryBot::DeclarationList.new
let(:attribute_list) { double("attribute list", define_attribute: true) }
expect(declaration_list.attributes).to be_a(FactoryBot::AttributeList)
end
it "defines each attribute on the attribute list" do
attribute1 = double("attribute 1")
attribute2 = double("attribute 2")
attribute3 = double("attribute 3")
declaration1 = double("declaration 1", to_attributes: [attribute1, attribute2])
declaration2 = double("declaration2", to_attributes: [attribute3])
attribute_list = double("attribute list", define_attribute: true)
declaration_list = FactoryBot::DeclarationList.new
allow(FactoryBot::AttributeList).to receive(:new).and_return attribute_list
subject.declare_attribute(declaration_1)
subject.declare_attribute(declaration_2)
declaration_list.declare_attribute(declaration1)
declaration_list.declare_attribute(declaration2)
subject.attributes
declaration_list.attributes
expect(attribute_list).to have_received(:define_attribute).with(attribute_1)
expect(attribute_list).to have_received(:define_attribute).with(attribute_2)
expect(attribute_list).to have_received(:define_attribute).with(attribute_3)
expect(attribute_list).to have_received(:define_attribute).with(attribute1)
expect(attribute_list).to have_received(:define_attribute).with(attribute2)
expect(attribute_list).to have_received(:define_attribute).with(attribute3)
end
end
describe FactoryBot::DeclarationList, "#declare_attribute" do
let(:declaration_1) { double("declaration", name: "declaration 1") }
let(:declaration_2) { double("declaration", name: "declaration 2") }
let(:declaration_with_same_name) do
double("declaration", name: "declaration 1")
it "adds the declaration to the list when not overridable" do
declaration1 = double("declaration", name: "declaration 1")
declaration2 = double("declaration", name: "declaration 2")
declaration_list = FactoryBot::DeclarationList.new
declaration_list.declare_attribute(declaration1)
expect(declaration_list.to_a).to eq [declaration1]
declaration_list.declare_attribute(declaration2)
expect(declaration_list.to_a).to eq [declaration1, declaration2]
end
context "when not overridable" do
it "adds the declaration to the list" do
subject.declare_attribute(declaration_1)
expect(subject.to_a).to eq [declaration_1]
it "adds the declaration to the list when overridable" do
declaration1 = double("declaration", name: "declaration 1")
declaration2 = double("declaration", name: "declaration 2")
declaration_list = FactoryBot::DeclarationList.new
declaration_list.overridable
subject.declare_attribute(declaration_2)
expect(subject.to_a).to eq [declaration_1, declaration_2]
end
declaration_list.declare_attribute(declaration1)
expect(declaration_list.to_a).to eq [declaration1]
declaration_list.declare_attribute(declaration2)
expect(declaration_list.to_a).to eq [declaration1, declaration2]
end
context "when overridable" do
before { subject.overridable }
it "deletes declarations with the same name when overridable" do
declaration1 = double("declaration", name: "declaration 1")
declaration2 = double("declaration", name: "declaration 2")
declaration_with_same_name = double("declaration", name: "declaration 1")
declaration_list = FactoryBot::DeclarationList.new
declaration_list.overridable
it "adds the declaration to the list" do
subject.declare_attribute(declaration_1)
expect(subject.to_a).to eq [declaration_1]
declaration_list.declare_attribute(declaration1)
expect(declaration_list.to_a).to eq [declaration1]
subject.declare_attribute(declaration_2)
expect(subject.to_a).to eq [declaration_1, declaration_2]
end
declaration_list.declare_attribute(declaration2)
expect(declaration_list.to_a).to eq [declaration1, declaration2]
it "deletes declarations with the same name" do
subject.declare_attribute(declaration_1)
expect(subject.to_a).to eq [declaration_1]
declaration_list.declare_attribute(declaration_with_same_name)
expect(declaration_list.to_a).to eq [declaration2, declaration_with_same_name]
end
subject.declare_attribute(declaration_2)
expect(subject.to_a).to eq [declaration_1, declaration_2]
it "appends declarations with the same name when NOT overridable" do
declaration1 = double("declaration", name: "declaration 1")
declaration2 = double("declaration", name: "declaration 2")
declaration_with_same_name = double("declaration", name: "declaration 1")
subject.declare_attribute(declaration_with_same_name)
expect(subject.to_a).to eq [declaration_2, declaration_with_same_name]
end
# DeclarationList's `@overridable` attr is set to false by default
declaration_list = FactoryBot::DeclarationList.new
declaration_list.declare_attribute(declaration1)
expect(declaration_list.to_a).to eq [declaration1]
declaration_list.declare_attribute(declaration2)
expect(declaration_list.to_a).to eq [declaration1, declaration2]
declaration_list.declare_attribute(declaration_with_same_name)
expect(declaration_list.to_a).to eq [declaration1, declaration2, declaration_with_same_name]
end
end