2010-11-12 15:58:25 -05:00
|
|
|
describe "a created 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
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("User")
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2018-10-07 21:45:51 -04: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
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
subject { create("post") }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
it { should_not be_new_record }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
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
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
describe "a created instance, specifying strategy: :build" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2012-03-09 15:42:28 -05:00
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("User")
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("Post", user_id: :integer) do
|
2012-03-09 15:42:28 -05:00
|
|
|
belongs_to :user
|
2011-08-10 16:26:29 -04:00
|
|
|
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
|
2012-03-09 17:20:38 -05:00
|
|
|
association(:user, strategy: :build)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 15:42:28 -05:00
|
|
|
subject { create(:post) }
|
2012-02-17 11:29:11 -05:00
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
it "saves associations (strategy: :build only affects build, not create)" 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
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
describe "a custom create" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-01-19 19:47:49 -05:00
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_class("User") do
|
2010-11-12 16:21:16 -05:00
|
|
|
def initialize
|
|
|
|
@persisted = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def persist
|
|
|
|
@persisted = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
@persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2010-11-12 16:21:16 -05:00
|
|
|
factory :user do
|
2018-10-07 18:02:54 -04:00
|
|
|
to_create(&:persist)
|
2010-11-12 16:21:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the custom create block instead of save" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:user)).to be_persisted
|
2010-11-12 16:21:16 -05:00
|
|
|
end
|
|
|
|
end
|
2011-10-12 11:35:10 -04:00
|
|
|
|
2017-09-28 08:17:17 -04:00
|
|
|
describe "a custom create passing in an evaluator" do
|
|
|
|
before do
|
|
|
|
define_class("User") do
|
|
|
|
attr_accessor :name
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2017-09-28 08:17:17 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
transient { creation_name { "evaluator" } }
|
2017-09-28 08:17:17 -04:00
|
|
|
|
|
|
|
to_create do |user, evaluator|
|
|
|
|
user.name = evaluator.creation_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the evaluator to the custom create block" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:user).name).to eq "evaluator"
|
2017-09-28 08:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-12 11:35:10 -04:00
|
|
|
describe "calling `create` with a block" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-10-12 11:35:10 -04:00
|
|
|
|
|
|
|
before do
|
2018-10-07 21:45:51 -04: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 created instance" do
|
2018-10-07 21:45:51 -04:00
|
|
|
create(:company, name: "thoughtbot") do |company|
|
|
|
|
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 created instance" do
|
|
|
|
expected = nil
|
2013-01-18 13:27:57 -05:00
|
|
|
result = create(: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-10-12 11:35:10 -04:00
|
|
|
end
|