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/acceptance/build_spec.rb

105 lines
2.2 KiB
Ruby
Raw Normal View History

describe "a built instance" do
include FactoryBot::Syntax::Methods
before do
define_model('User')
2012-03-09 17:20:38 -05:00
define_model('Post', user_id: :integer) do
belongs_to :user
end
FactoryBot.define do
factory :user
factory :post do
user
end
end
end
subject { build(:post) }
2013-01-18 13:27:57 -05:00
it { should be_new_record }
context "when the :use_parent_strategy config option has not been set" do
before { FactoryBot.use_parent_strategy = nil }
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
before { FactoryBot.use_parent_strategy = true }
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
FactoryBot.use_parent_strategy = true
2013-01-18 13:27:57 -05:00
expect(subject.user).to be_kind_of(User)
expect(subject.user).to be_new_record
end
end
describe "a built instance with strategy: :create" do
include FactoryBot::Syntax::Methods
before do
define_model('User')
2012-03-09 17:20:38 -05:00
define_model('Post', user_id: :integer) do
belongs_to :user
end
FactoryBot.define do
factory :user
factory :post do
association(:user, strategy: :create)
end
end
end
subject { build(:post) }
2013-01-18 13:27:57 -05:00
it { should be_new_record }
it "assigns and saves associations" do
2013-01-18 13:27:57 -05:00
expect(subject.user).to be_kind_of(User)
expect(subject.user).not_to be_new_record
end
end
describe "calling `build` with a block" do
include FactoryBot::Syntax::Methods
before do
2012-03-09 17:20:38 -05:00
define_model('Company', name: :string)
FactoryBot.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|
2013-01-18 13:27:57 -05:00
expect(company.name).to eq('thoughtbot')
end
end
it "returns the built instance" do
expected = nil
2013-01-18 13:27:57 -05:00
result = build(:company) do |company|
expected = company
"hello!"
2013-01-18 13:27:57 -05:00
end
expect(result).to eq expected
end
end