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
|
2011-08-12 22:06:10 -04:00
|
|
|
@instance = stub("instance", :id= => nil, :id => 42)
|
|
|
|
@class = stub("class", :new => @instance)
|
2009-04-28 10:39:32 -04:00
|
|
|
|
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
|
|
|
|
|
2011-06-27 16:44:37 -04:00
|
|
|
it "should be persisted" do
|
|
|
|
@stub.result(nil).should be_persisted
|
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
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-08-12 22:06:10 -04:00
|
|
|
@associated_factory = stub('associate-factory')
|
|
|
|
FactoryGirl.stubs(:factory_by_name => @associated_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
|
2011-08-12 22:06:10 -04:00
|
|
|
@instance.stubs(:owner => @user)
|
|
|
|
@associated_factory.stubs(:run => @user)
|
|
|
|
@stub.stubs(:set)
|
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
|
2011-08-12 22:06:10 -04:00
|
|
|
|
|
|
|
it "should set the owner as the user" do
|
|
|
|
@stub.should have_received(:set).with(:owner, @user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should create a stub correctly on the association" do
|
|
|
|
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, {})
|
|
|
|
end
|
2009-04-28 10:39:32 -04:00
|
|
|
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
|
2011-08-12 22:06:10 -04:00
|
|
|
@associated_factory.stubs(:run => @user)
|
2009-04-28 10:39:32 -04:00
|
|
|
@stub.association(:user).should == @user
|
2011-08-12 22:06:10 -04:00
|
|
|
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, {})
|
2009-04-28 10:39:32 -04:00
|
|
|
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
|
2011-08-12 22:06:10 -04:00
|
|
|
@spy = stub("after_stub callback", :foo => nil)
|
2009-10-10 00:46:19 -04:00
|
|
|
@stub.add_callback(:after_stub, proc{ @spy.foo })
|
2010-11-12 16:21:16 -05:00
|
|
|
@stub.result(nil)
|
2011-08-12 22:06:10 -04:00
|
|
|
@spy.should have_received(:foo)
|
2009-10-10 00:46:19 -04:00
|
|
|
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"
|
2011-08-12 22:06:10 -04:00
|
|
|
@instance.stubs(:attribute => @value, :attribute= => @value)
|
2009-04-28 10:39:32 -04:00
|
|
|
@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
|
|
|
|
|
2011-08-12 22:06:10 -04:00
|
|
|
it "should set the attribute as the value" do
|
|
|
|
@instance.should have_received(:attribute=).with(@value)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should retrieve the attribute" do
|
|
|
|
@stub.attribute
|
|
|
|
@instance.should have_received(:attribute)
|
|
|
|
end
|
|
|
|
|
2009-04-11 11:27:23 -04:00
|
|
|
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
|