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
Joshua Clayton 3282eea658 Move class creation/handling to an anonymous evaluator
This allows for Attribute#to_proc to not require a proxy to be passed to
return a Proc. It also allows for removal of Attribute#add_to
2011-11-30 20:56:58 -05:00

52 lines
1.4 KiB
Ruby

require 'spec_helper'
describe FactoryGirl::Attribute::Dynamic do
let(:name) { :first_name }
let(:block) { lambda { } }
subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) }
its(:name) { should == name }
context "with a block returning a static value" do
let(:block) { lambda { "value" } }
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
let(:block) { lambda {|thing| thing } }
it "returns self when executing the proc" do
subject.to_proc.call.should == subject
end
end
context "with a block referencing an attribute on the attribute" do
let(:block) { lambda { attribute_defined_on_attribute } }
let(:result) { "other attribute value" }
before do
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
let(:block) { lambda { Factory.sequence(:email) } }
it "raises a sequence abuse error" do
expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)
end
end
end
describe FactoryGirl::Attribute::Dynamic, "with a string name" do
subject { FactoryGirl::Attribute::Dynamic.new("name", false, lambda { } ) }
its(:name) { should == :name }
end