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

81 lines
2.1 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
@class = "class"
@instance = "instance"
stub(@class).new { @instance }
stub(@instance, :id=)
stub(@instance).id { 42 }
stub(@instance).reload { @instance.connection.reload }
@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 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"
stub(FactoryGirl).factory_by_name { @associated_factory }
@associated_factory = 'associate-factory'
2009-04-11 11:27:23 -04:00
end
describe "when asked to associate with another factory" do
before do
stub(@instance).owner { @user }
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
mock(@stub).set(:owner, @user)
2009-04-11 11:27:23 -04:00
@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
end
2009-04-11 11:27:23 -04:00
it "should return the association when building one" do
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
@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
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
2009-10-10 00:46:19 -04:00
@spy = Object.new
stub(@spy).foo
@stub.add_callback(:after_stub, proc{ @spy.foo })
@stub.result(nil)
2009-10-10 00:46:19 -04:00
@spy.should have_received.foo
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"
mock(@instance).send(:attribute) { @value }
mock(@instance).send(: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
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