mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
77 lines
2.8 KiB
Ruby
77 lines
2.8 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe FactoryGirl::Proxy do
|
|
subject { FactoryGirl::Proxy.new(Class.new) }
|
|
|
|
it "doesn't raise when assigning a value to an attribute" do
|
|
expect { subject.set(:name, "a name") }.to_not raise_error
|
|
end
|
|
|
|
it "returns nil for an attribute without a value" do
|
|
subject.get(:name).should be_nil
|
|
end
|
|
|
|
it "calls get for a missing method" do
|
|
subject.stubs(:get).with(:name).returns("it's a name")
|
|
subject.name.should == "it's a name"
|
|
end
|
|
|
|
it "doesn't raise when asked to associate with another factory" do
|
|
expect { subject.associate(:owner, :user, {}) }.to_not raise_error
|
|
end
|
|
|
|
it "raises an error when asking for the result" do
|
|
expect { subject.result(nil) }.to raise_error(NotImplementedError)
|
|
end
|
|
|
|
describe "when adding callbacks" do
|
|
let(:block_1) { proc { "block 1" } }
|
|
let(:block_2) { proc { "block 2" } }
|
|
|
|
it "adds a callback" do
|
|
subject.add_callback(:after_create, block_1)
|
|
subject.callbacks[:after_create].should be_eql([block_1])
|
|
end
|
|
|
|
it "adds multiple callbacks of the same name" do
|
|
subject.add_callback(:after_create, block_1)
|
|
subject.add_callback(:after_create, block_2)
|
|
subject.callbacks[:after_create].should be_eql([block_1, block_2])
|
|
end
|
|
|
|
it "adds multiple callbacks with different names" do
|
|
subject.add_callback(:after_create, block_1)
|
|
subject.add_callback(:after_build, block_2)
|
|
subject.callbacks[:after_create].should be_eql([block_1])
|
|
subject.callbacks[:after_build].should be_eql([block_2])
|
|
end
|
|
end
|
|
|
|
describe "when running callbacks" do
|
|
let(:object_1_within_callback) { stub("call_in_create", :foo => true) }
|
|
let(:object_2_within_callback) { stub("call_in_create", :foo => true) }
|
|
|
|
it "runs all callbacks with a given name" do
|
|
subject.add_callback(:after_create, proc { object_1_within_callback.foo })
|
|
subject.add_callback(:after_create, proc { object_2_within_callback.foo })
|
|
subject.run_callbacks(:after_create)
|
|
object_1_within_callback.should have_received(:foo).once
|
|
object_2_within_callback.should have_received(:foo).once
|
|
end
|
|
|
|
it "only runs callbacks with a given name" do
|
|
subject.add_callback(:after_create, proc { object_1_within_callback.foo })
|
|
subject.add_callback(:after_build, proc { object_2_within_callback.foo })
|
|
subject.run_callbacks(:after_create)
|
|
object_1_within_callback.should have_received(:foo).once
|
|
object_2_within_callback.should have_received(:foo).never
|
|
end
|
|
|
|
it "passes in the instance if the block takes an argument" do
|
|
subject.instance_variable_set("@instance", object_1_within_callback)
|
|
subject.add_callback(:after_create, proc {|spy| spy.foo })
|
|
subject.run_callbacks(:after_create)
|
|
object_1_within_callback.should have_received(:foo).once
|
|
end
|
|
end
|
|
end
|