2010-11-12 15:58:25 -05:00
|
|
|
describe "a built instance" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-01-19 19:47:49 -05:00
|
|
|
|
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
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.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
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
it { should be_new_record }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2016-12-16 05:28:09 -05:00
|
|
|
context "when the :use_parent_strategy config option has not been set" do
|
2017-10-20 15:20:28 -04:00
|
|
|
before { FactoryBot.use_parent_strategy = nil }
|
2016-12-16 05:28:09 -05:00
|
|
|
|
|
|
|
it "assigns and saves associations" do
|
|
|
|
expect(subject.user).to be_kind_of(User)
|
|
|
|
expect(subject.user).not_to be_new_record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the :use_parent_strategy config option has been enabled" do
|
2017-10-20 15:20:28 -04:00
|
|
|
before { FactoryBot.use_parent_strategy = true }
|
2016-12-16 05:28:09 -05:00
|
|
|
|
|
|
|
it "assigns but does not save associations" do
|
|
|
|
expect(subject.user).to be_kind_of(User)
|
|
|
|
expect(subject.user).to be_new_record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns but does not save associations when using parent strategy" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.use_parent_strategy = true
|
2016-12-16 05:28:09 -05:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.user).to be_kind_of(User)
|
2016-12-16 05:28:09 -05:00
|
|
|
expect(subject.user).to be_new_record
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-16 05:28:09 -05:00
|
|
|
describe "a built instance with strategy: :create" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-08-10 16:26:29 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-08-10 16:26:29 -04:00
|
|
|
factory :user
|
|
|
|
|
|
|
|
factory :post do
|
2016-12-16 05:28:09 -05:00
|
|
|
association(:user, strategy: :create)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { build(:post) }
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
it { should be_new_record }
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2016-12-16 05:28:09 -05:00
|
|
|
it "assigns and saves associations" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.user).to be_kind_of(User)
|
2016-12-16 05:28:09 -05:00
|
|
|
expect(subject.user).not_to be_new_record
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
2011-10-12 11:35:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "calling `build` with a block" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::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
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-10-12 11:35:10 -04:00
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the built instance" do
|
2012-03-09 17:20:38 -05:00
|
|
|
build(:company, name: 'thoughtbot') do |company|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(company.name).to eq('thoughtbot')
|
2011-10-12 11:35:10 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-20 13:10:52 -05:00
|
|
|
|
|
|
|
it "returns the built instance" do
|
|
|
|
expected = nil
|
2013-01-18 13:27:57 -05:00
|
|
|
result = build(:company) do |company|
|
2011-12-20 13:10:52 -05:00
|
|
|
expected = company
|
|
|
|
"hello!"
|
2013-01-18 13:27:57 -05:00
|
|
|
end
|
|
|
|
expect(result).to eq expected
|
2011-12-20 13:10:52 -05:00
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|