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::Proxy::Stub do
|
2009-04-11 11:27:23 -04:00
|
|
|
before do
|
2009-04-28 10:39:32 -04:00
|
|
|
@class = "class"
|
|
|
|
@instance = "instance"
|
|
|
|
stub(@class).new { @instance }
|
|
|
|
stub(@instance, :id=)
|
|
|
|
stub(@instance).id { 42 }
|
|
|
|
stub(@instance).reload { @instance.connection.reload }
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
@stub = FactoryGirl::Proxy::Stub.new(@class)
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be a new record" do
|
|
|
|
@stub.result.should_not be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be able to connect to the database" do
|
|
|
|
lambda { @stub.result.reload }.should raise_error(RuntimeError)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
describe "when a user factory exists" do
|
2009-04-11 11:27:23 -04:00
|
|
|
before do
|
2009-04-28 10:39:32 -04:00
|
|
|
@user = "user"
|
2010-06-24 09:45:57 -04:00
|
|
|
stub(FactoryGirl).factory_by_name { @associated_factory }
|
2010-06-11 15:00:14 -04:00
|
|
|
@associated_factory = 'associate-factory'
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
describe "when asked to associate with another factory" do
|
|
|
|
before do
|
|
|
|
stub(@instance).owner { @user }
|
2010-06-24 09:45:57 -04:00
|
|
|
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
2009-04-28 10:39:32 -04:00
|
|
|
mock(@stub).set(:owner, @user)
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
@stub.associate(:owner, :user, {})
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
it "should set a value for the association" do
|
|
|
|
@stub.result.owner.should == @user
|
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
it "should return the association when building one" do
|
2010-06-24 09:45:57 -04:00
|
|
|
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
2009-04-28 10:39:32 -04:00
|
|
|
@stub.association(:user).should == @user
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2009-10-10 00:46:19 -04:00
|
|
|
describe "when asked for the result" do
|
2009-10-10 01:23:57 -04:00
|
|
|
it "should return the actual instance" do
|
2009-10-10 00:46:19 -04:00
|
|
|
@stub.result.should == @instance
|
|
|
|
end
|
|
|
|
|
2009-10-10 01:23:57 -04:00
|
|
|
it "should run the :after_stub callback" do
|
2009-10-10 00:46:19 -04:00
|
|
|
@spy = Object.new
|
|
|
|
stub(@spy).foo
|
|
|
|
@stub.add_callback(:after_stub, proc{ @spy.foo })
|
|
|
|
@stub.result
|
|
|
|
@spy.should have_received.foo
|
|
|
|
end
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
describe "with an existing attribute" do
|
2009-04-11 11:27:23 -04:00
|
|
|
before do
|
2009-04-28 10:39:32 -04:00
|
|
|
@value = "value"
|
|
|
|
mock(@instance).send(:attribute) { @value }
|
|
|
|
mock(@instance).send(:attribute=, @value)
|
|
|
|
@stub.set(:attribute, @value)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
it "should to the resulting object" do
|
|
|
|
@stub.attribute.should == 'value'
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return that value when asked for that attribute" do
|
2009-04-28 10:39:32 -04:00
|
|
|
@stub.get(:attribute).should == @value
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|