2010-11-12 15:58:25 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "a created 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')
|
|
|
|
|
|
|
|
define_model('Post', :user_id => :integer) do
|
|
|
|
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 { create('post') }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
it "saves" do
|
|
|
|
should_not 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
|
|
|
|
|
2011-08-10 16:26:29 -04:00
|
|
|
describe "a created instance, specifying :method => build" 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
|
|
|
|
association(:user, :method => :build)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { create('post') }
|
|
|
|
|
|
|
|
it "still saves associations (:method => :build only affects build, not create)" do
|
|
|
|
subject.user.should be_kind_of(User)
|
|
|
|
subject.user.should_not be_new_record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
describe "a custom create" do
|
2011-01-19 19:47:49 -05:00
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
before do
|
|
|
|
define_class('User') do
|
|
|
|
def initialize
|
|
|
|
@persisted = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def persist
|
|
|
|
@persisted = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
@persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user do
|
|
|
|
to_create do |user|
|
|
|
|
user.persist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the custom create block instead of save" do
|
2011-01-26 20:55:06 -05:00
|
|
|
FactoryGirl.create(:user).should be_persisted
|
2010-11-12 16:21:16 -05:00
|
|
|
end
|
|
|
|
end
|
2011-10-12 11:35:10 -04:00
|
|
|
|
|
|
|
describe "calling `create` with a block" do
|
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
|
|
|
before do
|
|
|
|
define_model('Company', :name => :string)
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the created instance" do
|
|
|
|
create(:company, :name => 'thoughtbot') do |company|
|
|
|
|
company.name.should eq('thoughtbot')
|
|
|
|
end
|
|
|
|
end
|
2011-12-20 13:10:52 -05:00
|
|
|
|
|
|
|
it "returns the created instance" do
|
|
|
|
expected = nil
|
|
|
|
create(:company) do |company|
|
|
|
|
expected = company
|
|
|
|
"hello!"
|
|
|
|
end.should == expected
|
|
|
|
end
|
2011-10-12 11:35:10 -04:00
|
|
|
end
|