2012-01-20 13:04:48 -05:00
|
|
|
describe "initialize_with with non-FG attributes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2012-01-20 13:04:48 -05:00
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string, age: :integer) do
|
2012-01-20 13:04:48 -05:00
|
|
|
def self.construct(name, age)
|
2012-03-09 17:20:38 -05:00
|
|
|
new(name: name, age: age)
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-01-20 13:04:48 -05:00
|
|
|
factory :user do
|
2012-06-08 12:17:21 -04:00
|
|
|
initialize_with { User.construct("John Doe", 21) }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
subject { build(:user) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "John Doe" }
|
2020-06-05 15:15:18 -04:00
|
|
|
its(:age) { should eq 21 }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
|
2014-04-28 17:39:44 -04:00
|
|
|
describe "initialize_with with FG attributes that are transient" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2012-01-20 13:04:48 -05:00
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string) do
|
2012-01-20 13:04:48 -05:00
|
|
|
def self.construct(name)
|
2012-03-09 17:20:38 -05:00
|
|
|
new(name: "#{name} from .construct")
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-01-20 13:04:48 -05:00
|
|
|
factory :user do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2012-01-20 13:04:48 -05:00
|
|
|
name { "Handsome Chap" }
|
|
|
|
end
|
|
|
|
|
2012-06-08 12:17:21 -04:00
|
|
|
initialize_with { User.construct(name) }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
subject { build(:user) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Handsome Chap from .construct" }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "initialize_with non-ORM-backed objects" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2012-01-20 13:04:48 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
define_class("ReportGenerator") do
|
|
|
|
attr_reader :name, :data
|
|
|
|
|
|
|
|
def initialize(name, data)
|
|
|
|
@name = name
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2018-10-04 21:07:56 -04:00
|
|
|
sequence(:random_data) { Array.new(5) { Kernel.rand(200) } }
|
2012-01-20 13:04:48 -05:00
|
|
|
|
|
|
|
factory :report_generator do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "My Awesome Report" }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
initialize_with { ReportGenerator.new(name, FactoryBot.generate(:random_data)) }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows for overrides" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(build(:report_generator, name: "Overridden").name).to eq "Overridden"
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates random data" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(build(:report_generator).data.length).to eq 5
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "initialize_with parent and child factories" do
|
|
|
|
before do
|
|
|
|
define_class("Awesome") do
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-01-20 13:04:48 -05:00
|
|
|
factory :awesome do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Great" }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
|
2012-06-08 12:17:21 -04:00
|
|
|
initialize_with { Awesome.new(name) }
|
2012-01-20 13:04:48 -05:00
|
|
|
|
|
|
|
factory :sub_awesome do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Sub" }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :super_awesome do
|
2012-06-08 12:17:21 -04:00
|
|
|
initialize_with { Awesome.new("Super") }
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the parent's constructor when the child factory doesn't assign it" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:sub_awesome).name).to eq "Sub"
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows child factories to override initialize_with" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:super_awesome).name).to eq "Super"
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|
2012-04-23 23:48:33 -04:00
|
|
|
|
|
|
|
describe "initialize_with implicit constructor" do
|
|
|
|
before do
|
|
|
|
define_class("Awesome") do
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-04-23 23:48:33 -04:00
|
|
|
factory :awesome do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Great" }
|
2012-04-23 23:48:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
initialize_with { new(name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "instantiates the correct object" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:awesome, name: "Awesome name").name).to eq "Awesome name"
|
2012-04-23 23:48:33 -04:00
|
|
|
end
|
|
|
|
end
|
2012-05-18 14:28:51 -04:00
|
|
|
|
|
|
|
describe "initialize_with doesn't duplicate assignment on attributes accessed from initialize_with" do
|
|
|
|
before do
|
|
|
|
define_class("User") do
|
|
|
|
attr_reader :name
|
|
|
|
attr_accessor :email
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:email) { |n| "person#{n}@example.com" }
|
2012-05-18 14:28:51 -04:00
|
|
|
|
|
|
|
factory :user do
|
|
|
|
email
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
name { email.gsub(/@.+/, "") }
|
2012-05-18 14:28:51 -04:00
|
|
|
|
|
|
|
initialize_with { new(name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "instantiates the correct object" do
|
2017-10-20 15:20:28 -04:00
|
|
|
built_user = FactoryBot.build(:user)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(built_user.name).to eq "person1"
|
|
|
|
expect(built_user.email).to eq "person1@example.com"
|
2012-05-18 14:28:51 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-08 14:04:41 -04:00
|
|
|
|
|
|
|
describe "initialize_with has access to all attributes for construction" do
|
2018-10-22 22:29:02 -04:00
|
|
|
it "assigns attributes correctly" do
|
2012-06-08 14:04:41 -04:00
|
|
|
define_class("User") do
|
|
|
|
attr_reader :name, :email, :ignored
|
|
|
|
|
|
|
|
def initialize(attributes = {})
|
|
|
|
@name = attributes[:name]
|
|
|
|
@email = attributes[:email]
|
|
|
|
@ignored = attributes[:ignored]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:email) { |n| "person#{n}@example.com" }
|
2012-06-08 14:04:41 -04:00
|
|
|
|
|
|
|
factory :user do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2018-07-29 11:30:02 -04:00
|
|
|
ignored { "of course!" }
|
2012-06-08 14:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
email
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
name { email.gsub(/@.+/, "") }
|
2012-06-08 14:04:41 -04:00
|
|
|
|
2020-06-23 12:54:55 -04:00
|
|
|
initialize_with { new(**attributes) }
|
2012-06-08 14:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
user_with_attributes = FactoryBot.build(:user)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(user_with_attributes.email).to eq "person1@example.com"
|
|
|
|
expect(user_with_attributes.name).to eq "person1"
|
|
|
|
expect(user_with_attributes.ignored).to be_nil
|
2012-06-08 14:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
2016-02-06 00:31:26 -05:00
|
|
|
|
2018-10-22 22:29:02 -04:00
|
|
|
describe "initialize_with with an 'attributes' attribute" do
|
|
|
|
it "assigns attributes correctly" do
|
|
|
|
define_class("User") do
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(attributes:)
|
|
|
|
@name = attributes[:name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryBot.define do
|
|
|
|
factory :user do
|
2020-06-05 15:15:18 -04:00
|
|
|
attributes { {name: "Daniel"} }
|
2020-06-23 12:54:55 -04:00
|
|
|
initialize_with { new(**attributes) }
|
2018-10-22 22:29:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user = FactoryBot.build(:user)
|
|
|
|
|
|
|
|
expect(user.name).to eq("Daniel")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-06 00:31:26 -05:00
|
|
|
describe "initialize_with for a constructor that requires a block" do
|
|
|
|
it "executes the block correctly" do
|
|
|
|
define_class("Awesome") do
|
|
|
|
attr_reader :output
|
|
|
|
|
|
|
|
def initialize(&block)
|
|
|
|
@output = instance_exec(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2016-02-06 00:31:26 -05:00
|
|
|
factory :awesome do
|
|
|
|
initialize_with { new { "Output" } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:awesome).output).to eq "Output"
|
2016-02-06 00:31:26 -05:00
|
|
|
end
|
|
|
|
end
|
2020-07-15 16:47:21 -04:00
|
|
|
|
|
|
|
describe "initialize_with with a hash argument" do
|
|
|
|
it "builds the object correctly" do
|
|
|
|
define_class("Container") do
|
|
|
|
attr_reader :contents
|
|
|
|
|
|
|
|
def initialize(contents)
|
|
|
|
@contents = contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryBot.define do
|
|
|
|
factory :container do
|
|
|
|
initialize_with { new({key: :value}) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(FactoryBot.build(:container).contents).to eq({key: :value})
|
|
|
|
end
|
|
|
|
end
|