2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory do
|
2011-10-12 14:32:23 -04:00
|
|
|
it "has a factory name" do
|
2020-06-05 15:15:18 -04:00
|
|
|
name = :user
|
2020-01-17 14:26:30 -05:00
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
|
|
|
|
|
|
|
expect(factory.name).to eq name
|
2010-10-02 00:00:58 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "has a build class" do
|
2020-01-17 14:26:30 -05:00
|
|
|
name = :user
|
|
|
|
klass = define_class("User")
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
|
|
|
|
|
|
|
expect(factory.build_class).to eq klass
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "returns associations" do
|
Add functionality for enum traits (#1380)
## Enum traits
Given a Rails model with an enum attribute:
```rb
class Task < ActiveRecord::Base
enum status: {queued: 0, started: 1, finished: 2}
end
```
It is common for people to define traits for each possible value of the enum:
```rb
FactoryBot.define do
factory :task do
trait :queued do
status { :queued }
end
trait :started do
status { :started }
end
trait :finished do
status { :finished }
end
end
end
```
With this commit, those trait definitions are no longer necessary—they are defined automatically by factory_bot.
If automatically defining traits for enum attributes on every factory is not desired, it is possible to disable the feature by setting `FactoryBot.automatically_define_enum_traits = false` (see commit: [Allow opting out of automatically defining traits](https://github.com/thoughtbot/factory_bot/pull/1380/commits/5a20017351b08ce2ec9918d799e187e9eaa3ec32)).
In that case, it is still possible to explicitly define traits for an enum attribute in a particular factory:
```rb
FactoryBot.automatically_define_enum_traits = false
FactoryBot.define do
factory :task do
traits_for_enum(:status)
end
end
```
It is also possible to use this feature for other enumerable values, not specifically tied to ActiveRecord enum attributes:
```rb
class Task
attr_accessor :status
end
FactoryBot.define do
factory :task do
traits_for_enum(:status, ["queued", "started", "finished"])
end
end
```
The second argument here can be an enumerable object, including a Hash or Array.
Closes #1049
Co-authored-by: Lance Johnson <lancejjohnson@gmail.com>
Co-authored-by: PoTa <pota@mosfet.hu>
Co-authored-by: Frida Casas <fridacasas.fc@gmail.com>
Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
2020-05-01 17:50:51 -04:00
|
|
|
define_class("Post")
|
2017-10-20 15:20:28 -04:00
|
|
|
factory = FactoryBot::Factory.new(:post)
|
2019-04-26 15:48:37 -04:00
|
|
|
FactoryBot::Internal.register_factory(FactoryBot::Factory.new(:admin))
|
2017-10-20 15:20:28 -04:00
|
|
|
factory.declare_attribute(FactoryBot::Declaration::Association.new(:author, {}))
|
|
|
|
factory.declare_attribute(FactoryBot::Declaration::Association.new(:editor, {}))
|
|
|
|
factory.declare_attribute(FactoryBot::Declaration::Implicit.new(:admin, factory))
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.associations.each do |association|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(association).to be_association
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(factory.associations.to_a.length).to eq 3
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2012-02-07 18:55:57 -05:00
|
|
|
it "includes associations from the parent factory" do
|
2017-10-20 15:20:28 -04:00
|
|
|
association_on_parent = FactoryBot::Declaration::Association.new(:association_on_parent, {})
|
2020-06-05 15:15:18 -04:00
|
|
|
association_on_child = FactoryBot::Declaration::Association.new(:association_on_child, {})
|
2012-02-08 00:23:04 -05:00
|
|
|
|
Add functionality for enum traits (#1380)
## Enum traits
Given a Rails model with an enum attribute:
```rb
class Task < ActiveRecord::Base
enum status: {queued: 0, started: 1, finished: 2}
end
```
It is common for people to define traits for each possible value of the enum:
```rb
FactoryBot.define do
factory :task do
trait :queued do
status { :queued }
end
trait :started do
status { :started }
end
trait :finished do
status { :finished }
end
end
end
```
With this commit, those trait definitions are no longer necessary—they are defined automatically by factory_bot.
If automatically defining traits for enum attributes on every factory is not desired, it is possible to disable the feature by setting `FactoryBot.automatically_define_enum_traits = false` (see commit: [Allow opting out of automatically defining traits](https://github.com/thoughtbot/factory_bot/pull/1380/commits/5a20017351b08ce2ec9918d799e187e9eaa3ec32)).
In that case, it is still possible to explicitly define traits for an enum attribute in a particular factory:
```rb
FactoryBot.automatically_define_enum_traits = false
FactoryBot.define do
factory :task do
traits_for_enum(:status)
end
end
```
It is also possible to use this feature for other enumerable values, not specifically tied to ActiveRecord enum attributes:
```rb
class Task
attr_accessor :status
end
FactoryBot.define do
factory :task do
traits_for_enum(:status, ["queued", "started", "finished"])
end
end
```
The second argument here can be an enumerable object, including a Hash or Array.
Closes #1049
Co-authored-by: Lance Johnson <lancejjohnson@gmail.com>
Co-authored-by: PoTa <pota@mosfet.hu>
Co-authored-by: Frida Casas <fridacasas.fc@gmail.com>
Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
2020-05-01 17:50:51 -04:00
|
|
|
define_class("Post")
|
2017-10-20 15:20:28 -04:00
|
|
|
factory = FactoryBot::Factory.new(:post)
|
2012-02-08 00:23:04 -05:00
|
|
|
factory.declare_attribute(association_on_parent)
|
2019-04-26 15:48:37 -04:00
|
|
|
FactoryBot::Internal.register_factory(factory)
|
2012-02-08 00:23:04 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
child_factory = FactoryBot::Factory.new(:child_post, parent: :post)
|
2012-02-08 00:23:04 -05:00
|
|
|
child_factory.declare_attribute(association_on_child)
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(child_factory.associations.map(&:name)).to eq [:association_on_parent, :association_on_child]
|
2012-02-07 18:55:57 -05:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe "when overriding generated attributes with a hash" do
|
2011-10-12 14:32:23 -04:00
|
|
|
it "returns the overridden value in the generated attributes" do
|
2020-01-17 14:26:30 -05:00
|
|
|
name = :name
|
|
|
|
value = "The price is right!"
|
2020-06-05 15:15:18 -04:00
|
|
|
hash = {name => value}
|
2020-01-17 14:26:30 -05:00
|
|
|
define_class("Name")
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
2018-08-04 10:56:37 -04:00
|
|
|
declaration =
|
2020-01-17 14:26:30 -05:00
|
|
|
FactoryBot::Declaration::Dynamic.new(name, false, -> { flunk })
|
|
|
|
factory.declare_attribute(declaration)
|
|
|
|
result = factory.run(FactoryBot::Strategy::AttributesFor, hash)
|
|
|
|
expect(result[name]).to eq value
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "overrides a symbol parameter with a string parameter" do
|
2020-01-17 14:26:30 -05:00
|
|
|
name = :name
|
|
|
|
define_class("Name")
|
|
|
|
value = "The price is right!"
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
2018-08-04 10:56:37 -04:00
|
|
|
declaration =
|
2020-01-17 14:26:30 -05:00
|
|
|
FactoryBot::Declaration::Dynamic.new(name, false, -> { flunk })
|
|
|
|
factory.declare_attribute(declaration)
|
2020-06-05 15:15:18 -04:00
|
|
|
hash = {name.to_s => value}
|
2020-01-17 14:26:30 -05:00
|
|
|
result = factory.run(FactoryBot::Strategy::AttributesFor, hash)
|
|
|
|
|
|
|
|
expect(result[name]).to eq value
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe "overriding an attribute with an alias" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "uses the passed in value for the alias" do
|
|
|
|
name = :user
|
|
|
|
define_class("User")
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
2018-08-04 10:56:37 -04:00
|
|
|
attribute = FactoryBot::Declaration::Dynamic.new(
|
|
|
|
:test,
|
|
|
|
false, -> { "original" }
|
|
|
|
)
|
2020-01-17 14:26:30 -05:00
|
|
|
factory.declare_attribute(attribute)
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.aliases << [/(.*)_alias/, '\1']
|
2020-01-17 14:26:30 -05:00
|
|
|
result = factory.run(
|
2018-08-04 10:56:37 -04:00
|
|
|
FactoryBot::Strategy::AttributesFor,
|
2020-06-05 15:15:18 -04:00
|
|
|
test_alias: "new"
|
2018-08-04 10:56:37 -04:00
|
|
|
)
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(result[:test_alias]).to eq "new"
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "discards the predefined value for the attribute" do
|
2020-01-17 14:26:30 -05:00
|
|
|
name = :user
|
|
|
|
define_class("User")
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
|
|
|
attribute = FactoryBot::Declaration::Dynamic.new(
|
|
|
|
:test,
|
|
|
|
false, -> { "original" }
|
|
|
|
)
|
|
|
|
factory.declare_attribute(attribute)
|
|
|
|
FactoryBot.aliases << [/(.*)_alias/, '\1']
|
|
|
|
result = factory.run(
|
|
|
|
FactoryBot::Strategy::AttributesFor,
|
2020-06-05 15:15:18 -04:00
|
|
|
test_alias: "new"
|
2020-01-17 14:26:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(result[:test]).to be_nil
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "guesses the build class from the factory name" do
|
2020-01-17 14:26:30 -05:00
|
|
|
name = :user
|
|
|
|
define_class("User")
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
|
|
|
|
|
|
|
expect(factory.build_class).to eq User
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "creates a new factory using the class of the parent" do
|
2020-01-17 14:26:30 -05:00
|
|
|
name = :user
|
|
|
|
define_class("User")
|
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
|
|
|
|
|
|
|
child = FactoryBot::Factory.new(:child, parent: factory.name)
|
2011-10-28 10:59:49 -04:00
|
|
|
child.compile
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(child.build_class).to eq factory.build_class
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "creates a new factory while overriding the parent class" do
|
2020-06-05 15:15:18 -04:00
|
|
|
name = :user
|
Add functionality for enum traits (#1380)
## Enum traits
Given a Rails model with an enum attribute:
```rb
class Task < ActiveRecord::Base
enum status: {queued: 0, started: 1, finished: 2}
end
```
It is common for people to define traits for each possible value of the enum:
```rb
FactoryBot.define do
factory :task do
trait :queued do
status { :queued }
end
trait :started do
status { :started }
end
trait :finished do
status { :finished }
end
end
end
```
With this commit, those trait definitions are no longer necessary—they are defined automatically by factory_bot.
If automatically defining traits for enum attributes on every factory is not desired, it is possible to disable the feature by setting `FactoryBot.automatically_define_enum_traits = false` (see commit: [Allow opting out of automatically defining traits](https://github.com/thoughtbot/factory_bot/pull/1380/commits/5a20017351b08ce2ec9918d799e187e9eaa3ec32)).
In that case, it is still possible to explicitly define traits for an enum attribute in a particular factory:
```rb
FactoryBot.automatically_define_enum_traits = false
FactoryBot.define do
factory :task do
traits_for_enum(:status)
end
end
```
It is also possible to use this feature for other enumerable values, not specifically tied to ActiveRecord enum attributes:
```rb
class Task
attr_accessor :status
end
FactoryBot.define do
factory :task do
traits_for_enum(:status, ["queued", "started", "finished"])
end
end
```
The second argument here can be an enumerable object, including a Hash or Array.
Closes #1049
Co-authored-by: Lance Johnson <lancejjohnson@gmail.com>
Co-authored-by: PoTa <pota@mosfet.hu>
Co-authored-by: Frida Casas <fridacasas.fc@gmail.com>
Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
2020-05-01 17:50:51 -04:00
|
|
|
define_class("User")
|
2020-01-17 14:26:30 -05:00
|
|
|
factory = FactoryBot::Factory.new(name)
|
|
|
|
FactoryBot::Internal.register_factory(factory)
|
|
|
|
|
|
|
|
child = FactoryBot::Factory.new(:child, class: String, parent: factory.name)
|
2011-10-28 10:59:49 -04:00
|
|
|
child.compile
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(child.build_class).to eq String
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "when defined with a custom class" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "is an instance of that custom class" do
|
|
|
|
factory = FactoryBot::Factory.new(:author, class: Float)
|
|
|
|
expect(factory.build_class).to eq Float
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "when given a class that overrides #to_s" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "sets build_class correctly" do
|
2011-12-14 17:10:32 -05:00
|
|
|
define_class("Overriding")
|
|
|
|
define_class("Overriding::Class") do
|
|
|
|
def self.to_s
|
|
|
|
"Overriding"
|
|
|
|
end
|
|
|
|
end
|
2020-01-17 14:26:30 -05:00
|
|
|
overriding_class = Overriding::Class
|
|
|
|
factory = FactoryBot::Factory.new(:overriding_class, class: Overriding::Class)
|
2011-12-14 17:10:32 -05:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(factory.build_class).to eq overriding_class
|
2011-12-14 17:10:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "when defined with a class instead of a name" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "has a name" do
|
|
|
|
klass = ArgumentError
|
|
|
|
name = :argument_error
|
|
|
|
factory = FactoryBot::Factory.new(klass)
|
|
|
|
|
|
|
|
expect(factory.name).to eq name
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "has a build_class" do
|
|
|
|
klass = ArgumentError
|
|
|
|
factory = FactoryBot::Factory.new(klass)
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(factory.build_class).to eq klass
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "when defined with a custom class name" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "has a build_class equal to its custom class name" do
|
|
|
|
factory = FactoryBot::Factory.new(:author, class: :argument_error)
|
|
|
|
|
|
|
|
expect(factory.build_class).to eq ArgumentError
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "with a name ending in s" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "has a name" do
|
|
|
|
factory = FactoryBot::Factory.new(:business)
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(factory.name).to eq :business
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "has a build class" do
|
|
|
|
define_class("Business")
|
|
|
|
factory = FactoryBot::Factory.new(:business)
|
|
|
|
|
|
|
|
expect(factory.build_class).to eq Business
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-03-17 18:49:35 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "with a string for a name" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "has a name" do
|
|
|
|
name = :string
|
|
|
|
factory = FactoryBot::Factory.new(name.to_s)
|
|
|
|
|
|
|
|
expect(factory.name).to eq name
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-03-17 18:49:35 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "for namespaced class" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "sets build_class correctly with a namespaced class with Namespace::Class syntax" do
|
|
|
|
name = :settings
|
2011-08-13 01:03:12 -04:00
|
|
|
define_class("Admin")
|
|
|
|
define_class("Admin::Settings")
|
2020-01-17 14:26:30 -05:00
|
|
|
settings_class = Admin::Settings
|
|
|
|
factory = FactoryBot::Factory.new(name, class: "Admin::Settings")
|
2011-08-13 01:03:12 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(factory.build_class).to eq settings_class
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "sets build_class correctly with a namespaced class with namespace/class syntax" do
|
|
|
|
name = :settings
|
|
|
|
define_class("Admin")
|
|
|
|
define_class("Admin::Settings")
|
|
|
|
settings_class = Admin::Settings
|
|
|
|
factory = FactoryBot::Factory.new(name, class: "admin/settings")
|
2011-08-13 01:03:12 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
expect(factory.build_class).to eq settings_class
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Factory, "human names" do
|
2020-01-17 14:26:30 -05:00
|
|
|
it "parses names without underscores" do
|
|
|
|
factory = FactoryBot::Factory.new(:user)
|
|
|
|
|
|
|
|
expect(factory.names).to eq [:user]
|
2011-06-30 18:27:25 -04:00
|
|
|
end
|
2010-10-02 00:00:58 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "parses human names without underscores" do
|
|
|
|
factory = FactoryBot::Factory.new(:user)
|
|
|
|
|
|
|
|
expect(factory.human_names).to eq ["user"]
|
2010-10-02 00:00:58 -04:00
|
|
|
end
|
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "parses names with underscores" do
|
|
|
|
factory = FactoryBot::Factory.new(:happy_user)
|
|
|
|
|
|
|
|
expect(factory.names).to eq [:happy_user]
|
2011-10-27 15:45:21 -04:00
|
|
|
end
|
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "parses human names with underscores" do
|
|
|
|
factory = FactoryBot::Factory.new(:happy_user)
|
|
|
|
|
|
|
|
expect(factory.human_names).to eq ["happy user"]
|
2011-06-30 18:27:25 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "parses names with big letters" do
|
|
|
|
factory = FactoryBot::Factory.new(:LoL)
|
|
|
|
|
|
|
|
expect(factory.names).to eq [:LoL]
|
2018-08-04 10:56:37 -04:00
|
|
|
end
|
2020-01-17 14:26:30 -05:00
|
|
|
|
|
|
|
it "parses human names with big letters" do
|
|
|
|
factory = FactoryBot::Factory.new(:LoL)
|
|
|
|
|
|
|
|
expect(factory.human_names).to eq ["lol"]
|
2018-08-04 10:56:37 -04:00
|
|
|
end
|
2020-01-17 14:26:30 -05:00
|
|
|
|
|
|
|
it "parses names with aliases" do
|
|
|
|
factory = FactoryBot::Factory.new(:happy_user, aliases: [:gleeful_user, :person])
|
|
|
|
|
|
|
|
expect(factory.names).to eq [:happy_user, :gleeful_user, :person]
|
2017-06-01 12:54:02 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2020-01-17 14:26:30 -05:00
|
|
|
it "parses human names with aliases" do
|
|
|
|
factory = FactoryBot::Factory.new(:happy_user, aliases: [:gleeful_user, :person])
|
|
|
|
|
|
|
|
expect(factory.human_names).to eq ["happy user", "gleeful user", "person"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe FactoryBot::Factory, "running a factory" do
|
|
|
|
def build_factory
|
|
|
|
attribute = FactoryBot::Attribute::Dynamic.new(:name, false, -> { "value" })
|
|
|
|
attributes = [attribute]
|
|
|
|
declaration = FactoryBot::Declaration::Dynamic.new(:name, false, -> { "value" })
|
|
|
|
strategy = double("strategy", result: "result", add_observer: true)
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string)
|
2020-06-05 15:15:18 -04:00
|
|
|
allow(FactoryBot::Declaration::Dynamic).to receive(:new)
|
|
|
|
.and_return declaration
|
2017-06-01 12:54:02 -04:00
|
|
|
allow(declaration).to receive(:to_attributes).and_return attributes
|
2017-10-20 15:20:28 -04:00
|
|
|
allow(FactoryBot::Strategy::Build).to receive(:new).and_return strategy
|
2020-01-17 14:26:30 -05:00
|
|
|
factory = FactoryBot::Factory.new(:user)
|
|
|
|
factory.declare_attribute(declaration)
|
|
|
|
factory
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
it "creates the right strategy using the build class when running" do
|
2020-01-17 14:26:30 -05:00
|
|
|
factory = build_factory
|
|
|
|
factory.run(FactoryBot::Strategy::Build, {})
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot::Strategy::Build).to have_received(:new).once
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
it "returns the result from the strategy when running" do
|
2020-01-17 14:26:30 -05:00
|
|
|
factory = build_factory
|
|
|
|
|
|
|
|
expect(factory.run(FactoryBot::Strategy::Build, {})).to eq "result"
|
2011-08-20 17:08:15 -04:00
|
|
|
end
|
2011-10-26 23:51:25 -04:00
|
|
|
|
|
|
|
it "calls the block and returns the result" do
|
2020-01-17 14:26:30 -05:00
|
|
|
factory = build_factory
|
|
|
|
|
2011-10-26 23:51:25 -04:00
|
|
|
block_run = nil
|
2018-10-05 14:54:08 -04:00
|
|
|
block = ->(_result) { block_run = "changed" }
|
2020-01-17 14:26:30 -05:00
|
|
|
factory.run(FactoryBot::Strategy::Build, {}, &block)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(block_run).to eq "changed"
|
2011-10-26 23:51:25 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|