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/support/shared_examples/proxy.rb
2011-08-19 17:21:55 -04:00

91 lines
2.8 KiB
Ruby

shared_examples_for "proxy without association support" do
it "doesn't raise when asked to associate with another factory" do
expect { subject.associate(:owner, :user, {}) }.to_not raise_error
end
it "does not call FactoryGirl.create when building an association" do
FactoryGirl.stubs(:create)
subject.association(:user)
FactoryGirl.should have_received(:create).never
end
it "returns nil when building an association" do
subject.set(:association, 'x')
subject.association(:user).should be_nil
end
end
shared_examples_for "proxy with association support" do |factory_girl_proxy_class|
let(:factory_name) { :user }
let(:association_name) { :owner }
let(:factory) { stub("associate_factory") }
let(:overrides) { { :one => 1, :two => 2 } }
before do
FactoryGirl.stubs(:factory_by_name => factory)
instance.stubs(association_name => factory_name)
factory.stubs(:run => factory_name)
subject.stubs(:set)
end
it "sets a value for the association" do
subject.associate(association_name, factory_name, {})
subject.result(nil).send(association_name).should == factory_name
end
it "sets the association attribute as the factory" do
subject.associate(association_name, factory_name, {})
subject.should have_received(:set).with(association_name, factory_name)
end
it "runs the factory with the correct proxy class" do
subject.associate(association_name, factory_name, {})
factory.should have_received(:run).with(factory_girl_proxy_class, {})
end
it "runs the factory with the correct proxy class and overrides" do
subject.associate(association_name, factory_name, overrides)
factory.should have_received(:run).with(factory_girl_proxy_class, overrides)
end
end
shared_examples_for "proxy with standard getters and setters" do |attribute, value|
before do
instance.stubs(:"#{attribute}=" => value, :"#{attribute}" => value)
end
describe "when setting an attribute" do
before do
subject.set(attribute, value)
end
its(attribute) { should == value }
it { instance.should have_received(:"#{attribute}=").with(value) }
end
describe "when getting an attribute" do
it { subject.get(attribute).should == value }
it "retrieves the value from the instance" do
subject.get(attribute)
instance.should have_received(:"#{attribute}")
end
end
end
shared_examples_for "proxy with callbacks" do |callback_name|
let(:callback) { stub("#{callback_name} callback", :foo => nil) }
before do
subject.add_callback(callback_name, proc { callback.foo })
end
it "runs the #{callback_name} callback" do
subject.result(nil)
callback.should have_received(:foo).once
end
it "returns the proxy instance" do
subject.result(nil).should == instance
end
end