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

53 lines
1.5 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2009-04-11 11:27:23 -04:00
describe FactoryGirl::Attribute::Dynamic do
let(:name) { :first_name }
2012-04-20 16:59:39 -04:00
let(:block) { -> { } }
2009-04-11 11:27:23 -04:00
subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) }
2009-04-11 11:27:23 -04:00
its(:name) { should == name }
2009-04-11 11:27:23 -04:00
context "with a block returning a static value" do
2012-04-20 16:59:39 -04:00
let(:block) { -> { "value" } }
2009-04-11 11:27:23 -04:00
it "returns the value when executing the proc" do
subject.to_proc.call.should == "value"
end
end
context "with a block returning its block-level variable" do
2012-04-20 16:59:39 -04:00
let(:block) { ->(thing) { thing } }
it "returns self when executing the proc" do
subject.to_proc.call.should == subject
end
2009-04-11 11:27:23 -04:00
end
context "with a block referencing an attribute on the attribute" do
2012-04-20 16:59:39 -04:00
let(:block) { -> { attribute_defined_on_attribute } }
let(:result) { "other attribute value" }
before do
2012-03-09 17:20:38 -05:00
subject.stubs(attribute_defined_on_attribute: result)
end
it "evaluates the attribute from the attribute" do
subject.to_proc.call.should == result
end
end
context "with a block returning a sequence" do
2012-04-20 16:59:39 -04:00
let(:block) { -> { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) {|n| "foo#{n}" }) } }
it "raises a sequence abuse error" do
expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)
end
2009-04-11 11:27:23 -04:00
end
end
describe FactoryGirl::Attribute::Dynamic, "with a string name" do
2012-04-20 16:59:39 -04:00
subject { FactoryGirl::Attribute::Dynamic.new("name", false, -> { } ) }
its(:name) { should == :name }
end