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/factory_girl/proxy/stub_spec.rb

96 lines
2.6 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2009-04-11 11:27:23 -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)
@stub = FactoryGirl::Proxy::Stub.new(@class)
end
it "should not be a new record" do
@stub.result(nil).should_not be_new_record
end
it "should be persisted" do
@stub.result(nil).should be_persisted
end
it "should not be able to connect to the database" do
lambda { @stub.result(nil).reload }.should raise_error(RuntimeError)
2009-04-11 11:27:23 -04:00
end
describe "when a user factory exists" do
2009-04-11 11:27:23 -04:00
before do
@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
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)
@stub.associate(:owner, :user, {})
end
2009-04-11 11:27:23 -04:00
it "should set a value for the association" do
@stub.result(nil).owner.should == @user
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
end
2009-04-11 11:27:23 -04:00
it "should return the association when building one" do
2011-08-12 22:06:10 -04:00
@associated_factory.stubs(:run => @user)
@stub.association(:user).should == @user
2011-08-12 22:06:10 -04:00
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, {})
end
2009-04-11 11:27:23 -04:00
2009-10-10 00:46:19 -04:00
describe "when asked for the result" do
it "should return the actual instance" do
@stub.result(nil).should == @instance
2009-10-10 00:46:19 -04:00
end
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 })
@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
end
2009-04-11 11:27:23 -04:00
end
describe "with an existing attribute" do
2009-04-11 11:27:23 -04:00
before do
@value = "value"
2011-08-12 22:06:10 -04:00
@instance.stubs(:attribute => @value, :attribute= => @value)
@stub.set(:attribute, @value)
2009-04-11 11:27:23 -04:00
end
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
@stub.get(:attribute).should == @value
2009-04-11 11:27:23 -04:00
end
end
end