2011-08-10 16:26:29 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "a stubbed instance" do
|
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
|
|
|
before do
|
|
|
|
define_model('User')
|
|
|
|
|
|
|
|
define_model('Post', :user_id => :integer) do
|
|
|
|
belongs_to :user
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user
|
|
|
|
|
|
|
|
factory :post do
|
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { build_stubbed(:post) }
|
|
|
|
|
|
|
|
it "acts as if it came from the database" do
|
|
|
|
should_not be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns associations and acts as if it is saved" do
|
|
|
|
subject.user.should be_kind_of(User)
|
|
|
|
subject.user.should_not be_new_record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
describe "a stubbed instance overriding strategy" do
|
2011-08-10 16:26:29 -04:00
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
def define_factories_with_method
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
factory :post do
|
|
|
|
association(:user, :method => :build)
|
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
2012-02-17 11:29:11 -05:00
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
def define_factories_with_strategy
|
2011-08-10 16:26:29 -04:00
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user
|
|
|
|
|
|
|
|
factory :post do
|
2012-02-17 11:29:11 -05:00
|
|
|
association(:user, :strategy => :build)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
before do
|
|
|
|
define_model('User')
|
|
|
|
define_model('Post', :user_id => :integer) do
|
|
|
|
belongs_to :user
|
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
context "associations declared with :strategy" do
|
|
|
|
before { define_factories_with_strategy }
|
|
|
|
subject { build_stubbed(:post) }
|
|
|
|
|
|
|
|
it "acts as if it is saved in the database" do
|
|
|
|
should_not be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns associations and acts as if it is saved" do
|
|
|
|
subject.user.should be_kind_of(User)
|
|
|
|
subject.user.should_not be_new_record
|
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
context "associations declared with :method" do
|
|
|
|
before { define_factories_with_method }
|
|
|
|
subject { build_stubbed(:post) }
|
|
|
|
|
|
|
|
it "acts as if it is saved in the database" do
|
|
|
|
should_not be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns associations and acts as if it is saved" do
|
|
|
|
subject.user.should be_kind_of(User)
|
|
|
|
subject.user.should_not be_new_record
|
|
|
|
end
|
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|