thoughtbot--factory_bot/spec/factory_bot/attribute/dynamic_spec.rb

66 lines
1.8 KiB
Ruby
Raw Normal View History

describe FactoryBot::Attribute::Dynamic do
let(:name) { :first_name }
let(:block) { -> {} }
2009-04-11 15:27:23 +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
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
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"
end
end
context "with a block returning its block-level variable" do
2012-04-20 20:59:39 +00:00
let(:block) { ->(thing) { thing } }
it "returns self when executing the proc" do
2013-01-18 18:27:57 +00:00
expect(subject.to_proc.call).to eq subject
end
2009-04-11 15:27:23 +00:00
end
context "with a block referencing an attribute on the attribute" do
let(:block) { -> { attribute_defined_on_attribute } }
let(:result) { "other attribute value" }
before do
# Define an '#attribute_defined_on_attribute' instance method allowing it
# be mocked. Ususually this is determined via '#method_missing'
missing_methods = Module.new {
def attribute_defined_on_attribute(*args)
end
}
subject.extend(missing_methods)
allow(subject)
.to receive(:attribute_defined_on_attribute).and_return result
end
it "evaluates the attribute from the attribute" do
2013-01-18 18:27:57 +00:00
expect(subject.to_proc.call).to eq result
end
end
context "with a block returning a sequence" do
let(:block) do
-> do
FactoryBot::Internal.register_sequence(
FactoryBot::Sequence.new(:email, 1) { |n| "foo#{n}" }
)
end
end
it "raises a sequence abuse error" do
expect { subject.to_proc.call }.to raise_error(FactoryBot::SequenceAbuseError)
end
2009-04-11 15:27:23 +00:00
end
end
describe FactoryBot::Attribute::Dynamic, "with a string name" do
subject { FactoryBot::Attribute::Dynamic.new("name", false, -> {}) }
2013-01-18 18:27:57 +00:00
its(:name) { should eq :name }
end