2010-11-12 15:58:25 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "a built instance" do
|
2011-01-19 19:47:49 -05:00
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
2010-11-12 15:58:25 -05:00
|
|
|
before do
|
|
|
|
define_model('User')
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Post', user_id: :integer) do
|
2010-11-12 15:58:25 -05:00
|
|
|
belongs_to :user
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
2011-06-29 14:43:15 -04:00
|
|
|
factory :user
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
factory :post do
|
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-19 19:47:49 -05:00
|
|
|
subject { build(:post) }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
it "isn't saved" do
|
|
|
|
should be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns and saves associations" do
|
|
|
|
subject.user.should be_kind_of(User)
|
|
|
|
subject.user.should_not be_new_record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
describe "a built instance with strategy: :build" do
|
2011-08-10 16:26:29 -04:00
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
|
|
|
before do
|
|
|
|
define_model('User')
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Post', user_id: :integer) do
|
2011-08-10 16:26:29 -04:00
|
|
|
belongs_to :user
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user
|
|
|
|
|
|
|
|
factory :post do
|
2012-03-09 17:20:38 -05:00
|
|
|
association(:user, strategy: :build)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { build(:post) }
|
|
|
|
|
|
|
|
it "isn't saved" do
|
|
|
|
should be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns but does not save associations" do
|
|
|
|
subject.user.should be_kind_of(User)
|
|
|
|
subject.user.should be_new_record
|
|
|
|
end
|
2011-10-12 11:35:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "calling `build` with a block" do
|
|
|
|
include FactoryGirl::Syntax::Methods
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2011-10-12 11:35:10 -04:00
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Company', name: :string)
|
2011-10-12 11:35:10 -04:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the built instance" do
|
2012-03-09 17:20:38 -05:00
|
|
|
build(:company, name: 'thoughtbot') do |company|
|
2011-10-12 11:35:10 -04:00
|
|
|
company.name.should eq('thoughtbot')
|
|
|
|
end
|
|
|
|
end
|
2011-12-20 13:10:52 -05:00
|
|
|
|
|
|
|
it "returns the built instance" do
|
|
|
|
expected = nil
|
|
|
|
build(:company) do |company|
|
|
|
|
expected = company
|
|
|
|
"hello!"
|
|
|
|
end.should == expected
|
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|