2017-10-20 19:20:28 +00:00
|
|
|
describe FactoryBot::Attribute::Dynamic do
|
2020-06-05 19:15:18 +00:00
|
|
|
let(:name) { :first_name }
|
2018-09-28 01:35:05 +00:00
|
|
|
let(:block) { -> {} }
|
2009-04-11 15:27:23 +00:00
|
|
|
|
2017-10-20 19:20:28 +00:00
|
|
|
subject { FactoryBot::Attribute::Dynamic.new(name, false, block) }
|
2009-04-11 15:27:23 +00:00
|
|
|
|
2013-01-18 18:27:57 +00:00
|
|
|
its(:name) { should eq name }
|
2009-04-11 15:27:23 +00:00
|
|
|
|
2011-08-13 05:03:12 +00:00
|
|
|
context "with a block returning a static value" do
|
2012-04-20 20:59:39 +00:00
|
|
|
let(:block) { -> { "value" } }
|
2009-04-11 15:27:23 +00:00
|
|
|
|
2011-11-22 23:01:01 +00:00
|
|
|
it "returns the value when executing the proc" do
|
2013-01-18 18:27:57 +00:00
|
|
|
expect(subject.to_proc.call).to eq "value"
|
2011-08-13 05:03:12 +00:00
|
|
|
end
|
2010-10-01 03:35:24 +00:00
|
|
|
end
|
|
|
|
|
2011-08-13 05:03:12 +00:00
|
|
|
context "with a block returning its block-level variable" do
|
2012-04-20 20:59:39 +00:00
|
|
|
let(:block) { ->(thing) { thing } }
|
2011-08-13 05:03:12 +00:00
|
|
|
|
2011-11-29 03:52:54 +00:00
|
|
|
it "returns self when executing the proc" do
|
2013-01-18 18:27:57 +00:00
|
|
|
expect(subject.to_proc.call).to eq subject
|
2011-08-13 05:03:12 +00:00
|
|
|
end
|
2009-04-11 15:27:23 +00:00
|
|
|
end
|
|
|
|
|
2011-11-29 20:20:08 +00:00
|
|
|
context "with a block referencing an attribute on the attribute" do
|
2020-06-05 19:15:18 +00:00
|
|
|
let(:block) { -> { attribute_defined_on_attribute } }
|
2011-08-13 05:03:12 +00:00
|
|
|
let(:result) { "other attribute value" }
|
|
|
|
|
2017-07-14 15:45:59 +00:00
|
|
|
before do
|
|
|
|
# Define an '#attribute_defined_on_attribute' instance method allowing it
|
|
|
|
# be mocked. Ususually this is determined via '#method_missing'
|
2021-02-20 21:55:25 +00:00
|
|
|
missing_methods = Module.new {
|
|
|
|
def attribute_defined_on_attribute(*args)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
subject.extend(missing_methods)
|
2017-07-14 15:45:59 +00:00
|
|
|
|
2020-06-05 19:15:18 +00:00
|
|
|
allow(subject)
|
|
|
|
.to receive(:attribute_defined_on_attribute).and_return result
|
2011-08-13 05:03:12 +00:00
|
|
|
end
|
|
|
|
|
2011-11-29 20:20:08 +00:00
|
|
|
it "evaluates the attribute from the attribute" do
|
2013-01-18 18:27:57 +00:00
|
|
|
expect(subject.to_proc.call).to eq result
|
2011-08-13 05:03:12 +00:00
|
|
|
end
|
2009-04-14 01:39:19 +00:00
|
|
|
end
|
|
|
|
|
2011-08-13 05:03:12 +00:00
|
|
|
context "with a block returning a sequence" do
|
2019-04-26 19:48:37 +00:00
|
|
|
let(:block) do
|
|
|
|
-> do
|
|
|
|
FactoryBot::Internal.register_sequence(
|
2020-06-05 19:15:18 +00:00
|
|
|
FactoryBot::Sequence.new(:email, 1) { |n| "foo#{n}" }
|
2019-04-26 19:48:37 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2011-08-13 05:03:12 +00:00
|
|
|
|
|
|
|
it "raises a sequence abuse error" do
|
2017-10-20 19:20:28 +00:00
|
|
|
expect { subject.to_proc.call }.to raise_error(FactoryBot::SequenceAbuseError)
|
2011-08-13 05:03:12 +00:00
|
|
|
end
|
2009-04-11 15:27:23 +00:00
|
|
|
end
|
|
|
|
end
|
2011-08-13 05:03:12 +00:00
|
|
|
|
2017-10-20 19:20:28 +00:00
|
|
|
describe FactoryBot::Attribute::Dynamic, "with a string name" do
|
2020-06-05 19:15:18 +00:00
|
|
|
subject { FactoryBot::Attribute::Dynamic.new("name", false, -> {}) }
|
2013-01-18 18:27:57 +00:00
|
|
|
its(:name) { should eq :name }
|
2011-08-13 05:03:12 +00:00
|
|
|
end
|