2010-06-10 13:37:51 -04:00
|
|
|
require 'spec_helper'
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Attribute::Dynamic do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:name) { :first_name }
|
2012-04-20 16:59:39 -04:00
|
|
|
let(:block) { -> { } }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-07 18:19:27 -04:00
|
|
|
subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
its(:name) { should == name }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -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
|
|
|
|
2011-11-22 18:01:01 -05:00
|
|
|
it "returns the value when executing the proc" do
|
2011-11-29 15:20:08 -05:00
|
|
|
subject.to_proc.call.should == "value"
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
2010-09-30 23:35:24 -04:00
|
|
|
end
|
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
context "with a block returning its block-level variable" do
|
2012-04-20 16:59:39 -04:00
|
|
|
let(:block) { ->(thing) { thing } }
|
2011-08-13 01:03:12 -04:00
|
|
|
|
2011-11-28 22:52:54 -05:00
|
|
|
it "returns self when executing the proc" do
|
2011-11-29 15:20:08 -05:00
|
|
|
subject.to_proc.call.should == subject
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-11-29 15:20:08 -05:00
|
|
|
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 } }
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:result) { "other attribute value" }
|
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject.stubs(attribute_defined_on_attribute: result)
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
|
|
|
|
2011-11-29 15:20:08 -05:00
|
|
|
it "evaluates the attribute from the attribute" do
|
|
|
|
subject.to_proc.call.should == result
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
2009-04-13 21:39:19 -04:00
|
|
|
end
|
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
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}" }) } }
|
2011-08-13 01:03:12 -04:00
|
|
|
|
|
|
|
it "raises a sequence abuse error" do
|
2011-11-29 15:20:08 -05:00
|
|
|
expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-13 01:03:12 -04:00
|
|
|
|
|
|
|
describe FactoryGirl::Attribute::Dynamic, "with a string name" do
|
2012-04-20 16:59:39 -04:00
|
|
|
subject { FactoryGirl::Attribute::Dynamic.new("name", false, -> { } ) }
|
2011-08-13 01:03:12 -04:00
|
|
|
its(:name) { should == :name }
|
|
|
|
end
|