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
|
2010-11-12 16:21:16 -05:00
|
|
|
@stub.result(nil).should_not be_new_record
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be able to connect to the database" do
|
2010-11-12 16:21:16 -05:00
|
|
|
lambda { @stub.result(nil).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"
|
2011-05-19 10:56:45 -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
|
2010-11-12 16:21:16 -05:00
|
|
|
@stub.result(nil).owner.should == @user
|
2009-04-28 10:39:32 -04:00
|
|
|
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
|
2010-11-12 16:21:16 -05:00
|
|
|
@stub.result(nil).should == @instance
|
2009-10-10 00:46:19 -04:00
|
|
|
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 })
|
2010-11-12 16:21:16 -05:00
|
|
|
@stub.result(nil)
|
2009-10-10 00:46:19 -04:00
|
|
|
@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
|