Run standardrb
This commit applies the changes from running `standardrb --fix`
This commit is contained in:
parent
0c75dc27cd
commit
5f1a1de114
2
Rakefile
2
Rakefile
|
@ -8,7 +8,7 @@ require "cucumber/rake/task"
|
|||
Bundler::GemHelper.install_tasks(name: "factory_bot")
|
||||
|
||||
desc "Default: run the specs and features."
|
||||
task default: %w(spec:unit spec:acceptance features)
|
||||
task default: %w[spec:unit spec:acceptance features]
|
||||
|
||||
namespace :spec do
|
||||
desc "Run unit specs"
|
||||
|
|
|
@ -2,9 +2,9 @@ $LOAD_PATH << File.expand_path("lib", __dir__)
|
|||
require "factory_bot/version"
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = "factory_bot"
|
||||
s.version = FactoryBot::VERSION
|
||||
s.summary = "factory_bot provides a framework and DSL for defining and "\
|
||||
s.name = "factory_bot"
|
||||
s.version = FactoryBot::VERSION
|
||||
s.summary = "factory_bot provides a framework and DSL for defining and "\
|
||||
"using model instance factories."
|
||||
s.description = "factory_bot provides a framework and DSL for defining and "\
|
||||
"using factories - less error-prone, more explicit, and "\
|
||||
|
@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|||
s.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
||||
|
||||
s.authors = ["Josh Clayton", "Joe Ferris"]
|
||||
s.email = ["jclayton@thoughtbot.com", "jferris@thoughtbot.com"]
|
||||
s.email = ["jclayton@thoughtbot.com", "jferris@thoughtbot.com"]
|
||||
|
||||
s.homepage = "https://github.com/thoughtbot/factory_bot"
|
||||
s.metadata["changelog_uri"] = "https://github.com/thoughtbot/factory_bot/blob/master/NEWS.md"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
Then /^I should find the following for the last category:$/ do |table|
|
||||
Then(/^I should find the following for the last category:$/) do |table|
|
||||
table.hashes.first.each do |key, value|
|
||||
expect(Category.last.attributes[key].to_s).to eq value
|
||||
end
|
||||
|
|
|
@ -7,30 +7,30 @@ end
|
|||
|
||||
World(FactoryBotDefinitionsHelper)
|
||||
|
||||
When /^"([^"]*)" is added to FactoryBot's file definitions path$/ do |file_name|
|
||||
When(/^"([^"]*)" is added to FactoryBot's file definitions path$/) do |file_name|
|
||||
new_factory_file = File.join(expand_path("."), file_name.gsub(".rb", ""))
|
||||
|
||||
append_file_to_factory_bot_definitions_path(new_factory_file)
|
||||
|
||||
step %{I find definitions}
|
||||
step %(I find definitions)
|
||||
end
|
||||
|
||||
When /^"([^"]*)" is added to FactoryBot's file definitions path as an absolute path$/ do |file_name|
|
||||
When(/^"([^"]*)" is added to FactoryBot's file definitions path as an absolute path$/) do |file_name|
|
||||
new_factory_file = File.expand_path(File.join(expand_path("."), file_name.gsub(".rb", "")))
|
||||
|
||||
append_file_to_factory_bot_definitions_path(new_factory_file)
|
||||
|
||||
step %{I find definitions}
|
||||
step %(I find definitions)
|
||||
end
|
||||
|
||||
When /^I create a "([^"]*)" instance from FactoryBot$/ do |factory_name|
|
||||
When(/^I create a "([^"]*)" instance from FactoryBot$/) do |factory_name|
|
||||
FactoryBot.create(factory_name)
|
||||
end
|
||||
|
||||
When /^I find definitions$/ do
|
||||
When(/^I find definitions$/) do
|
||||
FactoryBot.find_definitions
|
||||
end
|
||||
|
||||
When /^I reload factories$/ do
|
||||
When(/^I reload factories$/) do
|
||||
FactoryBot.reload
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
ActiveRecord::Base.establish_connection(
|
||||
adapter: "sqlite3",
|
||||
database: ":memory:",
|
||||
database: ":memory:"
|
||||
)
|
||||
|
||||
class CreateSchema < ActiveRecord::Migration[5.0]
|
||||
|
|
|
@ -81,10 +81,10 @@ module FactoryBot
|
|||
|
||||
class << self
|
||||
delegate :factories,
|
||||
:register_strategy,
|
||||
:rewind_sequences,
|
||||
:strategy_by_name,
|
||||
to: Internal
|
||||
:register_strategy,
|
||||
:rewind_sequences,
|
||||
:strategy_by_name,
|
||||
to: Internal
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -5,14 +5,14 @@ module FactoryBot
|
|||
|
||||
self.aliases = [
|
||||
[/(.+)_id/, '\1'],
|
||||
[/(.*)/, '\1_id'],
|
||||
[/(.*)/, '\1_id']
|
||||
]
|
||||
|
||||
def self.aliases_for(attribute)
|
||||
aliases.map do |(pattern, replace)|
|
||||
aliases.map { |(pattern, replace)|
|
||||
if pattern.match(attribute.to_s)
|
||||
attribute.to_s.sub(pattern, replace).to_sym
|
||||
end
|
||||
end.compact << attribute
|
||||
}.compact << attribute
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,12 +6,12 @@ module FactoryBot
|
|||
|
||||
def initialize(name, factory, overrides)
|
||||
super(name, false)
|
||||
@factory = factory
|
||||
@factory = factory
|
||||
@overrides = overrides
|
||||
end
|
||||
|
||||
def to_proc
|
||||
factory = @factory
|
||||
factory = @factory
|
||||
overrides = @overrides
|
||||
traits_and_overrides = [factory, overrides].flatten
|
||||
factory_name = traits_and_overrides.shift
|
||||
|
|
|
@ -14,7 +14,7 @@ module FactoryBot
|
|||
value = case block.arity
|
||||
when 1, -1 then instance_exec(self, &block)
|
||||
else instance_exec(&block)
|
||||
end
|
||||
end
|
||||
raise SequenceAbuseError if FactoryBot::Sequence === value
|
||||
|
||||
value
|
||||
|
|
|
@ -2,10 +2,10 @@ module FactoryBot
|
|||
# @api private
|
||||
class AttributeAssigner
|
||||
def initialize(evaluator, build_class, &instance_builder)
|
||||
@build_class = build_class
|
||||
@instance_builder = instance_builder
|
||||
@evaluator = evaluator
|
||||
@attribute_list = evaluator.class.attribute_list
|
||||
@build_class = build_class
|
||||
@instance_builder = instance_builder
|
||||
@evaluator = evaluator
|
||||
@attribute_list = evaluator.class.attribute_list
|
||||
@attribute_names_assigned = []
|
||||
end
|
||||
|
||||
|
@ -22,9 +22,8 @@ module FactoryBot
|
|||
def hash
|
||||
@evaluator.instance = build_hash
|
||||
|
||||
attributes_to_set_on_hash.reduce({}) do |result, attribute|
|
||||
attributes_to_set_on_hash.each_with_object({}) do |attribute, result|
|
||||
result[attribute] = get(attribute)
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -33,13 +32,13 @@ module FactoryBot
|
|||
def method_tracking_evaluator
|
||||
@method_tracking_evaluator ||= Decorator::AttributeHash.new(
|
||||
decorated_evaluator,
|
||||
attribute_names_to_assign,
|
||||
attribute_names_to_assign
|
||||
)
|
||||
end
|
||||
|
||||
def decorated_evaluator
|
||||
Decorator::InvocationTracker.new(
|
||||
Decorator::NewConstructor.new(@evaluator, @build_class),
|
||||
Decorator::NewConstructor.new(@evaluator, @build_class)
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -96,11 +95,11 @@ module FactoryBot
|
|||
end
|
||||
|
||||
def alias_names_to_ignore
|
||||
@attribute_list.non_ignored.flat_map do |attribute|
|
||||
@attribute_list.non_ignored.flat_map { |attribute|
|
||||
override_names.map do |override|
|
||||
attribute.name if ignorable_alias?(attribute, override)
|
||||
end
|
||||
end.compact
|
||||
}.compact
|
||||
end
|
||||
|
||||
def ignorable_alias?(attribute, override)
|
||||
|
|
|
@ -4,7 +4,7 @@ module FactoryBot
|
|||
include Enumerable
|
||||
|
||||
def initialize(name = nil, attributes = [])
|
||||
@name = name
|
||||
@name = name
|
||||
@attributes = attributes
|
||||
end
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ module FactoryBot
|
|||
attr_reader :name
|
||||
|
||||
def initialize(name, block)
|
||||
@name = name.to_sym
|
||||
@name = name.to_sym
|
||||
@block = block
|
||||
end
|
||||
|
||||
|
@ -11,7 +11,7 @@ module FactoryBot
|
|||
case block.arity
|
||||
when 1, -1 then syntax_runner.instance_exec(instance, &block)
|
||||
when 2 then syntax_runner.instance_exec(instance, evaluator, &block)
|
||||
else syntax_runner.instance_exec(&block)
|
||||
else syntax_runner.instance_exec(&block)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -7,16 +7,16 @@ module FactoryBot
|
|||
:inline_sequences,
|
||||
:sequences,
|
||||
:strategies,
|
||||
:traits,
|
||||
:traits
|
||||
)
|
||||
|
||||
def initialize
|
||||
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
|
||||
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Sequence"))
|
||||
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Trait"))
|
||||
@strategies = Registry.new("Strategy")
|
||||
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
|
||||
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Sequence"))
|
||||
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Trait"))
|
||||
@strategies = Registry.new("Strategy")
|
||||
@callback_names = Set.new
|
||||
@definition = Definition.new(:configuration)
|
||||
@definition = Definition.new(:configuration)
|
||||
@inline_sequences = []
|
||||
|
||||
to_create(&:save!)
|
||||
|
@ -24,7 +24,7 @@ module FactoryBot
|
|||
end
|
||||
|
||||
delegate :to_create, :skip_create, :constructor, :before, :after,
|
||||
:callback, :callbacks, to: :@definition
|
||||
:callback, :callbacks, to: :@definition
|
||||
|
||||
def initialize_with(&block)
|
||||
@definition.define_constructor(&block)
|
||||
|
|
|
@ -8,7 +8,7 @@ module FactoryBot
|
|||
attr_reader :name
|
||||
|
||||
def initialize(name, ignored = false)
|
||||
@name = name
|
||||
@name = name
|
||||
@ignored = ignored
|
||||
end
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ module FactoryBot
|
|||
|
||||
def initialize(name = nil)
|
||||
@declarations = []
|
||||
@name = name
|
||||
@overridable = false
|
||||
@name = name
|
||||
@overridable = false
|
||||
end
|
||||
|
||||
def declare_attribute(declaration)
|
||||
|
|
|
@ -4,17 +4,17 @@ module FactoryBot
|
|||
attr_reader :defined_traits, :declarations, :name, :registered_enums
|
||||
|
||||
def initialize(name, base_traits = [])
|
||||
@name = name
|
||||
@declarations = DeclarationList.new(name)
|
||||
@callbacks = []
|
||||
@defined_traits = Set.new
|
||||
@registered_enums = []
|
||||
@to_create = nil
|
||||
@base_traits = base_traits
|
||||
@name = name
|
||||
@declarations = DeclarationList.new(name)
|
||||
@callbacks = []
|
||||
@defined_traits = Set.new
|
||||
@registered_enums = []
|
||||
@to_create = nil
|
||||
@base_traits = base_traits
|
||||
@additional_traits = []
|
||||
@constructor = nil
|
||||
@attributes = nil
|
||||
@compiled = false
|
||||
@constructor = nil
|
||||
@attributes = nil
|
||||
@compiled = false
|
||||
end
|
||||
|
||||
delegate :declare_attribute, to: :declarations
|
||||
|
@ -51,7 +51,7 @@ module FactoryBot
|
|||
declarations.attributes
|
||||
|
||||
defined_traits.each do |defined_trait|
|
||||
base_traits.each { |bt| bt.define_trait defined_trait }
|
||||
base_traits.each { |bt| bt.define_trait defined_trait }
|
||||
additional_traits.each { |at| at.define_trait defined_trait }
|
||||
end
|
||||
|
||||
|
@ -128,7 +128,7 @@ module FactoryBot
|
|||
def initialize_copy(source)
|
||||
super
|
||||
@attributes = nil
|
||||
@compiled = false
|
||||
@compiled = false
|
||||
@defined_traits_by_name = nil
|
||||
end
|
||||
|
||||
|
@ -138,7 +138,7 @@ module FactoryBot
|
|||
[
|
||||
base_traits.map(&method_name),
|
||||
instance_exec(&block),
|
||||
additional_traits.map(&method_name),
|
||||
additional_traits.map(&method_name)
|
||||
].flatten.compact
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module FactoryBot
|
||||
class DefinitionProxy
|
||||
UNPROXIED_METHODS = %w(
|
||||
UNPROXIED_METHODS = %w[
|
||||
__send__
|
||||
__id__
|
||||
nil?
|
||||
|
@ -13,7 +13,7 @@ module FactoryBot
|
|||
raise
|
||||
caller
|
||||
method
|
||||
).freeze
|
||||
].freeze
|
||||
|
||||
(instance_methods + private_instance_methods).each do |method|
|
||||
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
|
||||
|
@ -24,8 +24,8 @@ module FactoryBot
|
|||
attr_reader :child_factories
|
||||
|
||||
def initialize(definition, ignore = false)
|
||||
@definition = definition
|
||||
@ignore = ignore
|
||||
@definition = definition
|
||||
@ignore = ignore
|
||||
@child_factories = []
|
||||
end
|
||||
|
||||
|
@ -152,7 +152,7 @@ module FactoryBot
|
|||
if block_given?
|
||||
raise AssociationDefinitionError.new(
|
||||
"Unexpected block passed to '#{name}' association "\
|
||||
"in '#{@definition.name}' factory",
|
||||
"in '#{@definition.name}' factory"
|
||||
)
|
||||
else
|
||||
declaration = Declaration::Association.new(name, *options)
|
||||
|
|
|
@ -23,9 +23,9 @@ module FactoryBot
|
|||
|
||||
def association(factory_name, *traits_and_overrides)
|
||||
overrides = traits_and_overrides.extract_options!
|
||||
strategy_override = overrides.fetch(:strategy) do
|
||||
strategy_override = overrides.fetch(:strategy) {
|
||||
FactoryBot.use_parent_strategy ? @build_strategy.class : :create
|
||||
end
|
||||
}
|
||||
|
||||
traits_and_overrides += [overrides.except(:strategy)]
|
||||
|
||||
|
@ -33,9 +33,7 @@ module FactoryBot
|
|||
@build_strategy.association(runner)
|
||||
end
|
||||
|
||||
def instance=(object_instance)
|
||||
@instance = object_instance
|
||||
end
|
||||
attr_writer :instance
|
||||
|
||||
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissingSuper
|
||||
if @instance.respond_to?(method_name)
|
||||
|
|
|
@ -3,7 +3,7 @@ module FactoryBot
|
|||
class EvaluatorClassDefiner
|
||||
def initialize(attributes, parent_class)
|
||||
@parent_class = parent_class
|
||||
@attributes = attributes
|
||||
@attributes = attributes
|
||||
|
||||
attributes.each do |attribute|
|
||||
evaluator_class.define_attribute(attribute.name, &attribute.to_proc)
|
||||
|
|
|
@ -8,23 +8,23 @@ module FactoryBot
|
|||
|
||||
def initialize(name, options = {})
|
||||
assert_valid_options(options)
|
||||
@name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
|
||||
@parent = options[:parent]
|
||||
@aliases = options[:aliases] || []
|
||||
@class_name = options[:class]
|
||||
@definition = Definition.new(@name, options[:traits] || [])
|
||||
@compiled = false
|
||||
@name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
|
||||
@parent = options[:parent]
|
||||
@aliases = options[:aliases] || []
|
||||
@class_name = options[:class]
|
||||
@definition = Definition.new(@name, options[:traits] || [])
|
||||
@compiled = false
|
||||
end
|
||||
|
||||
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
||||
:defined_traits, :inherit_traits, :append_traits, to: :@definition
|
||||
:defined_traits, :inherit_traits, :append_traits, to: :@definition
|
||||
|
||||
def build_class
|
||||
@build_class ||= if class_name.is_a? Class
|
||||
class_name
|
||||
else
|
||||
class_name.to_s.camelize.constantize
|
||||
end
|
||||
class_name
|
||||
else
|
||||
class_name.to_s.camelize.constantize
|
||||
end
|
||||
end
|
||||
|
||||
def run(build_strategy, overrides, &block)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
module FactoryBot
|
||||
class FactoryRunner
|
||||
def initialize(name, strategy, traits_and_overrides)
|
||||
@name = name
|
||||
@name = name
|
||||
@strategy = strategy
|
||||
|
||||
@overrides = traits_and_overrides.extract_options!
|
||||
@traits = traits_and_overrides
|
||||
@traits = traits_and_overrides
|
||||
end
|
||||
|
||||
def run(runner_strategy = @strategy, &block)
|
||||
|
@ -22,7 +22,7 @@ module FactoryBot
|
|||
strategy: runner_strategy,
|
||||
traits: @traits,
|
||||
overrides: @overrides,
|
||||
factory: factory,
|
||||
factory: factory
|
||||
}
|
||||
|
||||
ActiveSupport::Notifications.instrument("factory_bot.run_factory", instrumentation_payload) do
|
||||
|
|
|
@ -7,7 +7,7 @@ module FactoryBot
|
|||
attr_accessor :definition_file_paths
|
||||
end
|
||||
|
||||
self.definition_file_paths = %w(factories test/factories spec/factories)
|
||||
self.definition_file_paths = %w[factories test/factories spec/factories]
|
||||
|
||||
def self.find_definitions
|
||||
absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }
|
||||
|
|
|
@ -3,18 +3,18 @@ module FactoryBot
|
|||
module Internal
|
||||
class << self
|
||||
delegate :after,
|
||||
:before,
|
||||
:callbacks,
|
||||
:constructor,
|
||||
:factories,
|
||||
:initialize_with,
|
||||
:inline_sequences,
|
||||
:sequences,
|
||||
:skip_create,
|
||||
:strategies,
|
||||
:to_create,
|
||||
:traits,
|
||||
to: :configuration
|
||||
:before,
|
||||
:callbacks,
|
||||
:constructor,
|
||||
:factories,
|
||||
:initialize_with,
|
||||
:inline_sequences,
|
||||
:sequences,
|
||||
:skip_create,
|
||||
:strategies,
|
||||
:to_create,
|
||||
:traits,
|
||||
to: :configuration
|
||||
|
||||
def configuration
|
||||
@configuration ||= Configuration.new
|
||||
|
|
|
@ -19,17 +19,16 @@ module FactoryBot
|
|||
attr_reader :factories_to_lint, :invalid_factories, :factory_strategy
|
||||
|
||||
def calculate_invalid_factories
|
||||
factories_to_lint.reduce(Hash.new([])) do |result, factory|
|
||||
factories_to_lint.each_with_object(Hash.new([])) do |factory, result|
|
||||
errors = lint(factory)
|
||||
result[factory] |= errors unless errors.empty?
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
class FactoryError
|
||||
def initialize(wrapped_error, factory)
|
||||
@wrapped_error = wrapped_error
|
||||
@factory = factory
|
||||
@factory = factory
|
||||
end
|
||||
|
||||
def message
|
||||
|
@ -72,7 +71,7 @@ module FactoryBot
|
|||
result = []
|
||||
begin
|
||||
FactoryBot.public_send(factory_strategy, factory.name)
|
||||
rescue StandardError => e
|
||||
rescue => e
|
||||
result |= [FactoryError.new(e, factory)]
|
||||
end
|
||||
result
|
||||
|
@ -82,16 +81,16 @@ module FactoryBot
|
|||
result = []
|
||||
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
||||
FactoryBot.public_send(factory_strategy, factory.name, trait_name)
|
||||
rescue StandardError => e
|
||||
rescue => e
|
||||
result |= [FactoryTraitError.new(e, factory, trait_name)]
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def error_message
|
||||
lines = invalid_factories.map do |_factory, exceptions|
|
||||
lines = invalid_factories.map { |_factory, exceptions|
|
||||
exceptions.map(&error_message_type)
|
||||
end.flatten
|
||||
}.flatten
|
||||
|
||||
<<~ERROR_MESSAGE.strip
|
||||
The following factories are invalid:
|
||||
|
|
|
@ -8,14 +8,20 @@ module FactoryBot
|
|||
end
|
||||
|
||||
delegate :defined_traits, :callbacks, :attributes, :constructor,
|
||||
:to_create, to: :definition
|
||||
:to_create, to: :definition
|
||||
|
||||
def compile; end
|
||||
def compile
|
||||
end
|
||||
|
||||
def class_name; end
|
||||
def class_name
|
||||
end
|
||||
|
||||
def evaluator_class; FactoryBot::Evaluator; end
|
||||
def evaluator_class
|
||||
FactoryBot::Evaluator
|
||||
end
|
||||
|
||||
def hierarchy_class; FactoryBot::DefinitionHierarchy; end
|
||||
def hierarchy_class
|
||||
FactoryBot::DefinitionHierarchy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ module FactoryBot
|
|||
attr_reader :name
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@name = name
|
||||
@items = ActiveSupport::HashWithIndifferentAccess.new
|
||||
end
|
||||
|
||||
|
@ -25,7 +25,7 @@ module FactoryBot
|
|||
raise key_error_with_custom_message(e)
|
||||
end
|
||||
|
||||
alias :[] :find
|
||||
alias [] find
|
||||
|
||||
def register(name, item)
|
||||
@items[name] = item
|
||||
|
|
|
@ -6,14 +6,14 @@ module FactoryBot
|
|||
attr_reader :name
|
||||
|
||||
def initialize(name, *args, &proc)
|
||||
@name = name
|
||||
@proc = proc
|
||||
@name = name
|
||||
@proc = proc
|
||||
|
||||
options = args.extract_options!
|
||||
@value = args.first || 1
|
||||
options = args.extract_options!
|
||||
@value = args.first || 1
|
||||
@aliases = options.fetch(:aliases) { [] }
|
||||
|
||||
if !@value.respond_to?(:peek)
|
||||
unless @value.respond_to?(:peek)
|
||||
@value = EnumeratorAdapter.new(@value)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
module FactoryBot
|
||||
module Strategy
|
||||
class Null
|
||||
def association(runner); end
|
||||
def association(runner)
|
||||
end
|
||||
|
||||
def result(evaluation); end
|
||||
def result(evaluation)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,7 +21,7 @@ module FactoryBot
|
|||
:update_attributes!,
|
||||
:update_attributes,
|
||||
:update_column,
|
||||
:update_columns,
|
||||
:update_columns
|
||||
].freeze
|
||||
|
||||
def self.next_id=(id)
|
||||
|
@ -68,7 +68,7 @@ module FactoryBot
|
|||
DISABLED_PERSISTENCE_METHODS.each do |write_method|
|
||||
define_singleton_method(write_method) do |*args|
|
||||
raise "stubbed models are not allowed to access the database - "\
|
||||
"#{self.class}##{write_method}(#{args.join(',')})"
|
||||
"#{self.class}##{write_method}(#{args.join(",")})"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,12 +38,12 @@ module FactoryBot
|
|||
end
|
||||
|
||||
delegate :after,
|
||||
:before,
|
||||
:callback,
|
||||
:initialize_with,
|
||||
:skip_create,
|
||||
:to_create,
|
||||
to: FactoryBot::Internal
|
||||
:before,
|
||||
:callback,
|
||||
:initialize_with,
|
||||
:skip_create,
|
||||
:to_create,
|
||||
to: FactoryBot::Internal
|
||||
end
|
||||
|
||||
class ModifyDSL
|
||||
|
|
|
@ -15,7 +15,7 @@ module FactoryBot
|
|||
end
|
||||
|
||||
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
||||
:callbacks, :attributes, to: :@definition
|
||||
:callbacks, :attributes, to: :@definition
|
||||
|
||||
def names
|
||||
[@name]
|
||||
|
|
|
@ -12,7 +12,7 @@ describe "associations" do
|
|||
expect { FactoryBot.build(:post) }.to raise_error(
|
||||
ArgumentError,
|
||||
"Association 'author' received an invalid factory argument.\n" \
|
||||
"Did you mean? 'factory: :user'\n",
|
||||
"Did you mean? 'factory: :user'\n"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,8 +5,8 @@ describe "declaring attributes on a Factory that are private methods on Object"
|
|||
FactoryBot.define do
|
||||
factory :website do
|
||||
system { false }
|
||||
link { "http://example.com" }
|
||||
sleep { 15 }
|
||||
link { "http://example.com" }
|
||||
sleep { 15 }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -14,8 +14,8 @@ describe "declaring attributes on a Factory that are private methods on Object"
|
|||
subject { FactoryBot.build(:website, sleep: -5) }
|
||||
|
||||
its(:system) { should eq false }
|
||||
its(:link) { should eq "http://example.com" }
|
||||
its(:sleep) { should eq -5 }
|
||||
its(:link) { should eq "http://example.com" }
|
||||
its(:sleep) { should eq(-5) }
|
||||
end
|
||||
|
||||
describe "assigning overrides that are also private methods on object" do
|
||||
|
@ -25,7 +25,8 @@ describe "assigning overrides that are also private methods on object" do
|
|||
Object.class_eval do
|
||||
private
|
||||
|
||||
def some_funky_method(args); end
|
||||
def some_funky_method(args)
|
||||
end
|
||||
end
|
||||
|
||||
FactoryBot.define do
|
||||
|
@ -40,9 +41,9 @@ describe "assigning overrides that are also private methods on object" do
|
|||
end
|
||||
|
||||
subject { FactoryBot.build(:website, format: "Great", y: 12345, some_funky_method: "foobar!") }
|
||||
its(:format) { should eq "Great" }
|
||||
its(:y) { should eq 12345 }
|
||||
its(:more_format) { should eq "format: Great" }
|
||||
its(:format) { should eq "Great" }
|
||||
its(:y) { should eq 12345 }
|
||||
its(:more_format) { should eq "format: Great" }
|
||||
its(:some_funky_method) { should eq "foobar!" }
|
||||
end
|
||||
|
||||
|
@ -62,6 +63,6 @@ describe "accessing methods from the instance within a dynamic attribute "\
|
|||
end
|
||||
end
|
||||
|
||||
subject { FactoryBot.build(:website) }
|
||||
subject { FactoryBot.build(:website) }
|
||||
its(:more_format) { should eq "format: This is an awesome format" }
|
||||
end
|
||||
|
|
|
@ -69,10 +69,10 @@ describe "calling `attributes_for` with a block" do
|
|||
it "returns the hash of attributes" do
|
||||
expected = nil
|
||||
|
||||
result = attributes_for(:company) do |attributes|
|
||||
result = attributes_for(:company) { |attributes|
|
||||
expected = attributes
|
||||
"hello!"
|
||||
end
|
||||
}
|
||||
expect(result).to eq expected
|
||||
end
|
||||
end
|
||||
|
@ -80,7 +80,8 @@ end
|
|||
describe "`attributes_for` for a class whose constructor has required params" do
|
||||
before do
|
||||
define_model("User", name: :string) do
|
||||
def initialize(arg1, arg2); end
|
||||
def initialize(arg1, arg2)
|
||||
end
|
||||
end
|
||||
|
||||
FactoryBot.define do
|
||||
|
@ -90,6 +91,6 @@ describe "`attributes_for` for a class whose constructor has required params" do
|
|||
end
|
||||
end
|
||||
|
||||
subject { FactoryBot.attributes_for(:user) }
|
||||
subject { FactoryBot.attributes_for(:user) }
|
||||
its([:name]) { should eq "John Doe" }
|
||||
end
|
||||
|
|
|
@ -9,9 +9,9 @@ describe "a generated attributes hash where order matters" do
|
|||
|
||||
FactoryBot.define do
|
||||
factory :parent_model do
|
||||
evaluates_first { static }
|
||||
evaluates_first { static }
|
||||
evaluates_second { evaluates_first }
|
||||
evaluates_third { evaluates_second }
|
||||
evaluates_third { evaluates_second }
|
||||
|
||||
factory :child_model do
|
||||
static { 1 }
|
||||
|
@ -19,9 +19,9 @@ describe "a generated attributes hash where order matters" do
|
|||
end
|
||||
|
||||
factory :without_parent, class: ParentModel do
|
||||
evaluates_first { static }
|
||||
evaluates_second { evaluates_first }
|
||||
evaluates_third { evaluates_second }
|
||||
evaluates_first { static }
|
||||
evaluates_second { evaluates_first }
|
||||
evaluates_third { evaluates_second }
|
||||
static { 1 }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -88,10 +88,10 @@ describe "calling `build` with a block" do
|
|||
|
||||
it "returns the built instance" do
|
||||
expected = nil
|
||||
result = build(:company) do |company|
|
||||
result = build(:company) { |company|
|
||||
expected = company
|
||||
"hello!"
|
||||
end
|
||||
}
|
||||
expect(result).to eq expected
|
||||
end
|
||||
end
|
||||
|
|
|
@ -129,10 +129,10 @@ describe "calling `build_stubbed` with a block" do
|
|||
|
||||
it "returns the stub instance" do
|
||||
expected = nil
|
||||
result = build_stubbed(:company) do |company|
|
||||
result = build_stubbed(:company) { |company|
|
||||
expected = company
|
||||
"hello!"
|
||||
end
|
||||
}
|
||||
expect(result).to eq expected
|
||||
end
|
||||
end
|
||||
|
@ -160,8 +160,8 @@ describe "defaulting `created_at`" do
|
|||
end
|
||||
|
||||
it "doesn't add created_at to objects who don't have the method" do
|
||||
expect(build_stubbed(:thing_without_timestamp)).
|
||||
not_to respond_to(:created_at)
|
||||
expect(build_stubbed(:thing_without_timestamp))
|
||||
.not_to respond_to(:created_at)
|
||||
end
|
||||
|
||||
it "allows overriding created_at for objects with created_at" do
|
||||
|
@ -171,8 +171,8 @@ describe "defaulting `created_at`" do
|
|||
end
|
||||
|
||||
it "doesn't allow setting created_at on an object that doesn't define it" do
|
||||
expect { build_stubbed(:thing_without_timestamp, created_at: Time.now) }.
|
||||
to raise_error(NoMethodError, /created_at=/)
|
||||
expect { build_stubbed(:thing_without_timestamp, created_at: Time.now) }
|
||||
.to raise_error(NoMethodError, /created_at=/)
|
||||
end
|
||||
|
||||
it "allows assignment of created_at" do
|
||||
|
@ -224,8 +224,8 @@ describe "defaulting `updated_at`" do
|
|||
end
|
||||
|
||||
it "doesn't add updated_at to objects who don't have the method" do
|
||||
expect(build_stubbed(:thing_without_timestamp)).
|
||||
not_to respond_to(:updated_at)
|
||||
expect(build_stubbed(:thing_without_timestamp))
|
||||
.not_to respond_to(:updated_at)
|
||||
end
|
||||
|
||||
it "allows overriding updated_at for objects with updated_at" do
|
||||
|
@ -235,9 +235,9 @@ describe "defaulting `updated_at`" do
|
|||
end
|
||||
|
||||
it "doesn't allow setting updated_at on an object that doesn't define it" do
|
||||
expect do
|
||||
expect {
|
||||
build_stubbed(:thing_without_timestamp, updated_at: Time.now)
|
||||
end.to raise_error(NoMethodError, /updated_at=/)
|
||||
}.to raise_error(NoMethodError, /updated_at=/)
|
||||
end
|
||||
|
||||
it "allows assignment of updated_at" do
|
||||
|
|
|
@ -4,13 +4,13 @@ describe "callbacks" do
|
|||
|
||||
FactoryBot.define do
|
||||
factory :user_with_callbacks, class: :user do
|
||||
after(:stub) { |user| user.first_name = "Stubby" }
|
||||
after(:build) { |user| user.first_name = "Buildy" }
|
||||
after(:create) { |user| user.last_name = "Createy" }
|
||||
after(:stub) { |user| user.first_name = "Stubby" }
|
||||
after(:build) { |user| user.first_name = "Buildy" }
|
||||
after(:create) { |user| user.last_name = "Createy" }
|
||||
end
|
||||
|
||||
factory :user_with_inherited_callbacks, parent: :user_with_callbacks do
|
||||
after(:stub) { |user| user.last_name = "Double-Stubby" }
|
||||
after(:stub) { |user| user.last_name = "Double-Stubby" }
|
||||
after(:build) { |user| user.first_name = "Child-Buildy" }
|
||||
end
|
||||
end
|
||||
|
@ -79,8 +79,8 @@ describe "callbacks using syntax methods without referencing FactoryBot explicit
|
|||
sequence(:sequence_3)
|
||||
|
||||
factory :user do
|
||||
after(:stub) { generate(:sequence_3) }
|
||||
after(:build) { |user| user.first_number = generate(:sequence_1) }
|
||||
after(:stub) { generate(:sequence_3) }
|
||||
after(:build) { |user| user.first_number = generate(:sequence_1) }
|
||||
after(:create) { |user, _evaluator| user.last_number = generate(:sequence_2) }
|
||||
end
|
||||
end
|
||||
|
@ -145,13 +145,13 @@ describe "custom callbacks" do
|
|||
FactoryBot.define do
|
||||
factory :user do
|
||||
first_name { "John" }
|
||||
last_name { "Doe" }
|
||||
last_name { "Doe" }
|
||||
|
||||
before(:custom) { |instance| instance.first_name = "Overridden First" }
|
||||
after(:custom) { |instance| instance.last_name = "Overridden Last" }
|
||||
after(:custom) { |instance| instance.last_name = "Overridden Last" }
|
||||
callback(:totally_custom) do |instance|
|
||||
instance.first_name = "Totally"
|
||||
instance.last_name = "Custom"
|
||||
instance.last_name = "Custom"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -211,7 +211,7 @@ describe "global callbacks" do
|
|||
object.name = case object.class.to_s
|
||||
when "User" then "John Doe"
|
||||
when "Company" then "Acme Suppliers"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
after :create do |object|
|
||||
|
|
|
@ -125,10 +125,10 @@ describe "calling `create` with a block" do
|
|||
|
||||
it "returns the created instance" do
|
||||
expected = nil
|
||||
result = create(:company) do |company|
|
||||
result = create(:company) { |company|
|
||||
expected = company
|
||||
"hello!"
|
||||
end
|
||||
}
|
||||
expect(result).to eq expected
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,7 +14,7 @@ describe "defining methods inside FactoryBot" do
|
|||
|
||||
expect(bad_factory_definition).to raise_error(
|
||||
FactoryBot::MethodDefinitionError,
|
||||
/Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/,
|
||||
/Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ describe "an instance generated by a factory named a camel case string " do
|
|||
end
|
||||
|
||||
it "registers the UserModel factory" do
|
||||
expect(FactoryBot::Internal.factory_by_name("UserModel")).
|
||||
to be_a(FactoryBot::Factory)
|
||||
expect(FactoryBot::Internal.factory_by_name("UserModel"))
|
||||
.to be_a(FactoryBot::Factory)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,7 +24,7 @@ describe "attributes defined using Symbol#to_proc" do
|
|||
FactoryBot.define do
|
||||
factory :user do
|
||||
password { "foo" }
|
||||
password_confirmation &:password
|
||||
password_confirmation(&:password)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ describe "an instance generated by a factory" do
|
|||
end
|
||||
|
||||
it "registers the user factory" do
|
||||
expect(FactoryBot::Internal.factory_by_name(:user)).
|
||||
to be_a(FactoryBot::Factory)
|
||||
expect(FactoryBot::Internal.factory_by_name(:user))
|
||||
.to be_a(FactoryBot::Factory)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ describe "enum traits" do
|
|||
context "when automatically_define_enum_traits is true" do
|
||||
it "builds traits automatically for model enum field" do
|
||||
define_model("Task", status: :integer) do
|
||||
enum status: { queued: 0, started: 1, finished: 2 }
|
||||
enum status: {queued: 0, started: 1, finished: 2}
|
||||
end
|
||||
|
||||
FactoryBot.define do
|
||||
|
@ -20,7 +20,7 @@ describe "enum traits" do
|
|||
|
||||
it "prefers user defined traits over automatically built traits" do
|
||||
define_model("Task", status: :integer) do
|
||||
enum status: { queued: 0, started: 1, finished: 2 }
|
||||
enum status: {queued: 0, started: 1, finished: 2}
|
||||
end
|
||||
|
||||
FactoryBot.define do
|
||||
|
@ -49,7 +49,7 @@ describe "enum traits" do
|
|||
end
|
||||
|
||||
it "builds traits for each enumerated value using a provided list of values as a Hash" do
|
||||
statuses = { queued: 0, started: 1, finished: 2 }
|
||||
statuses = {queued: 0, started: 1, finished: 2}
|
||||
|
||||
define_class "Task" do
|
||||
attr_accessor :status
|
||||
|
@ -89,13 +89,13 @@ describe "enum traits" do
|
|||
end
|
||||
|
||||
it "builds traits for each enumerated value using a custom enumerable" do
|
||||
statuses = define_class("Statuses") do
|
||||
statuses = define_class("Statuses") {
|
||||
include Enumerable
|
||||
|
||||
def each(&block)
|
||||
["queued", "started", "finished"].each(&block)
|
||||
end
|
||||
end.new
|
||||
}.new
|
||||
|
||||
define_class "Task" do
|
||||
attr_accessor :status
|
||||
|
@ -119,7 +119,7 @@ describe "enum traits" do
|
|||
it "raises an error for undefined traits" do
|
||||
with_temporary_assignment(FactoryBot, :automatically_define_enum_traits, false) do
|
||||
define_model("Task", status: :integer) do
|
||||
enum status: { queued: 0, started: 1, finished: 2 }
|
||||
enum status: {queued: 0, started: 1, finished: 2}
|
||||
end
|
||||
|
||||
FactoryBot.define do
|
||||
|
@ -139,7 +139,7 @@ describe "enum traits" do
|
|||
it "builds traits for each enumerated value when traits_for_enum are specified" do
|
||||
with_temporary_assignment(FactoryBot, :automatically_define_enum_traits, false) do
|
||||
define_model("Task", status: :integer) do
|
||||
enum status: { queued: 0, started: 1, finished: 2 }
|
||||
enum status: {queued: 0, started: 1, finished: 2}
|
||||
end
|
||||
|
||||
FactoryBot.define do
|
||||
|
|
|
@ -15,9 +15,9 @@ describe "initialize_with with non-FG attributes" do
|
|||
end
|
||||
end
|
||||
|
||||
subject { build(:user) }
|
||||
subject { build(:user) }
|
||||
its(:name) { should eq "John Doe" }
|
||||
its(:age) { should eq 21 }
|
||||
its(:age) { should eq 21 }
|
||||
end
|
||||
|
||||
describe "initialize_with with FG attributes that are transient" do
|
||||
|
@ -41,7 +41,7 @@ describe "initialize_with with FG attributes that are transient" do
|
|||
end
|
||||
end
|
||||
|
||||
subject { build(:user) }
|
||||
subject { build(:user) }
|
||||
its(:name) { should eq "Handsome Chap from .construct" }
|
||||
end
|
||||
|
||||
|
@ -163,7 +163,7 @@ describe "initialize_with doesn't duplicate assignment on attributes accessed fr
|
|||
factory :user do
|
||||
email
|
||||
|
||||
name { email.gsub(/\@.+/, "") }
|
||||
name { email.gsub(/@.+/, "") }
|
||||
|
||||
initialize_with { new(name) }
|
||||
end
|
||||
|
@ -199,7 +199,7 @@ describe "initialize_with has access to all attributes for construction" do
|
|||
|
||||
email
|
||||
|
||||
name { email.gsub(/\@.+/, "") }
|
||||
name { email.gsub(/@.+/, "") }
|
||||
|
||||
initialize_with { new(attributes) }
|
||||
end
|
||||
|
@ -224,7 +224,7 @@ describe "initialize_with with an 'attributes' attribute" do
|
|||
|
||||
FactoryBot.define do
|
||||
factory :user do
|
||||
attributes { { name: "Daniel" } }
|
||||
attributes { {name: "Daniel"} }
|
||||
initialize_with { new(attributes) }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ describe "finding factories keyed by class instead of symbol" do
|
|||
|
||||
it "doesn't find the factory" do
|
||||
expect { FactoryBot.create(User) }.to(
|
||||
raise_error(KeyError, /Factory not registered: User/),
|
||||
raise_error(KeyError, /Factory not registered: User/)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,9 +21,9 @@ describe "FactoryBot.lint" do
|
|||
* admin_user - Validation failed: Name can't be blank (ActiveRecord::RecordInvalid)
|
||||
ERROR_MESSAGE
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint
|
||||
end.to raise_error FactoryBot::InvalidFactoryError, error_message
|
||||
}.to raise_error FactoryBot::InvalidFactoryError, error_message
|
||||
end
|
||||
|
||||
it "does not raise when all factories are valid" do
|
||||
|
@ -52,13 +52,13 @@ describe "FactoryBot.lint" do
|
|||
factory :invalid_thing
|
||||
end
|
||||
|
||||
expect do
|
||||
only_valid_factories = FactoryBot.factories.reject do |factory|
|
||||
expect {
|
||||
only_valid_factories = FactoryBot.factories.reject { |factory|
|
||||
factory.name =~ /invalid/
|
||||
end
|
||||
}
|
||||
|
||||
FactoryBot.lint only_valid_factories
|
||||
end.not_to raise_error
|
||||
}.not_to raise_error
|
||||
end
|
||||
|
||||
describe "trait validation" do
|
||||
|
@ -83,9 +83,9 @@ describe "FactoryBot.lint" do
|
|||
* user+unnamed - Validation failed: Name can't be blank (ActiveRecord::RecordInvalid)
|
||||
ERROR_MESSAGE
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint traits: true
|
||||
end.to raise_error FactoryBot::InvalidFactoryError, error_message
|
||||
}.to raise_error FactoryBot::InvalidFactoryError, error_message
|
||||
end
|
||||
|
||||
it "does not raise if a trait produces a valid object" do
|
||||
|
@ -102,9 +102,9 @@ describe "FactoryBot.lint" do
|
|||
end
|
||||
end
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint traits: true
|
||||
end.not_to raise_error
|
||||
}.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -123,10 +123,10 @@ describe "FactoryBot.lint" do
|
|||
end
|
||||
end
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint traits: false
|
||||
FactoryBot.lint
|
||||
end.not_to raise_error
|
||||
}.not_to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -147,9 +147,9 @@ describe "FactoryBot.lint" do
|
|||
end
|
||||
end
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint strategy: :build
|
||||
end.not_to raise_error
|
||||
}.not_to raise_error
|
||||
end
|
||||
|
||||
it "uses the requested strategy during trait validation" do
|
||||
|
@ -171,9 +171,9 @@ describe "FactoryBot.lint" do
|
|||
end
|
||||
end
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint traits: true, strategy: :build
|
||||
end.not_to raise_error
|
||||
}.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -189,11 +189,11 @@ describe "FactoryBot.lint" do
|
|||
factory :invalid_thing
|
||||
end
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.lint(verbose: true)
|
||||
end.to raise_error(
|
||||
}.to raise_error(
|
||||
FactoryBot::InvalidFactoryError,
|
||||
%r{#{__FILE__}:\d*:in `save!'},
|
||||
%r{#{__FILE__}:\d*:in `save!'}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -30,14 +30,14 @@ describe "modifying factories" do
|
|||
end
|
||||
end
|
||||
|
||||
subject { create(:user) }
|
||||
its(:name) { should eq "Great User" }
|
||||
subject { create(:user) }
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:login) { should eq "GREAT USER" }
|
||||
|
||||
it "doesn't allow the factory to be subsequently defined" do
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.define { factory :user }
|
||||
end.to raise_error(FactoryBot::DuplicateDefinitionError, "Factory already registered: user")
|
||||
}.to raise_error(FactoryBot::DuplicateDefinitionError, "Factory already registered: user")
|
||||
end
|
||||
|
||||
it "does allow the factory to be subsequently modified" do
|
||||
|
@ -66,7 +66,7 @@ describe "modifying factories" do
|
|||
|
||||
subject { create(:user) }
|
||||
|
||||
its(:name) { should eq "great user" }
|
||||
its(:name) { should eq "great user" }
|
||||
its(:login) { should be_nil }
|
||||
end
|
||||
|
||||
|
@ -86,9 +86,9 @@ describe "modifying factories" do
|
|||
end
|
||||
end
|
||||
|
||||
subject { create(:user) }
|
||||
subject { create(:user) }
|
||||
|
||||
its(:name) { should eq "Johnny Rockstar!!!" }
|
||||
its(:name) { should eq "Johnny Rockstar!!!" }
|
||||
its(:email) { should eq "Johnny Rockstar!!!@example.com" }
|
||||
its(:login) { should eq "JOHNNY ROCKSTAR!!!" }
|
||||
end
|
||||
|
@ -105,48 +105,48 @@ describe "modifying factories" do
|
|||
|
||||
context "creating user" do
|
||||
context "without overrides" do
|
||||
subject { create(:user) }
|
||||
subject { create(:user) }
|
||||
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:email) { should eq "Great User-modified@example.com" }
|
||||
end
|
||||
|
||||
context "overriding the email" do
|
||||
subject { create(:user, email: "perfect@example.com") }
|
||||
subject { create(:user, email: "perfect@example.com") }
|
||||
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:email) { should eq "perfect@example.com" }
|
||||
end
|
||||
|
||||
context "overriding the name" do
|
||||
subject { create(:user, name: "wonderful") }
|
||||
subject { create(:user, name: "wonderful") }
|
||||
|
||||
its(:name) { should eq "wonderful" }
|
||||
its(:name) { should eq "wonderful" }
|
||||
its(:email) { should eq "wonderful-modified@example.com" }
|
||||
end
|
||||
end
|
||||
|
||||
context "creating admin" do
|
||||
context "without overrides" do
|
||||
subject { create(:admin) }
|
||||
subject { create(:admin) }
|
||||
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:email) { should eq "Great User-modified@example.com" }
|
||||
its(:admin) { should be true }
|
||||
end
|
||||
|
||||
context "overriding the email" do
|
||||
subject { create(:admin, email: "perfect@example.com") }
|
||||
subject { create(:admin, email: "perfect@example.com") }
|
||||
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:name) { should eq "Great User" }
|
||||
its(:email) { should eq "perfect@example.com" }
|
||||
its(:admin) { should be true }
|
||||
end
|
||||
|
||||
context "overriding the name" do
|
||||
subject { create(:admin, name: "wonderful") }
|
||||
subject { create(:admin, name: "wonderful") }
|
||||
|
||||
its(:name) { should eq "wonderful" }
|
||||
its(:name) { should eq "wonderful" }
|
||||
its(:email) { should eq "wonderful-modified@example.com" }
|
||||
its(:admin) { should be true }
|
||||
end
|
||||
|
|
|
@ -4,11 +4,11 @@ describe "modifying inherited factories with traits" do
|
|||
FactoryBot.define do
|
||||
factory :user do
|
||||
trait(:female) { gender { "Female" } }
|
||||
trait(:male) { gender { "Male" } }
|
||||
trait(:male) { gender { "Male" } }
|
||||
|
||||
trait(:young_admin) do
|
||||
admin { true }
|
||||
age { 17 }
|
||||
age { 17 }
|
||||
end
|
||||
|
||||
female
|
||||
|
|
|
@ -30,7 +30,7 @@ describe "attribute overrides" do
|
|||
let(:admin) { FactoryBot.create(:admin) }
|
||||
|
||||
let(:post_attributes) do
|
||||
{ secure: false }
|
||||
{secure: false}
|
||||
end
|
||||
|
||||
let(:non_admin_post_attributes) do
|
||||
|
@ -44,17 +44,17 @@ describe "attribute overrides" do
|
|||
end
|
||||
|
||||
context "with an admin posting" do
|
||||
subject { FactoryBot.create(:post, admin_post_attributes) }
|
||||
subject { FactoryBot.create(:post, admin_post_attributes) }
|
||||
its(:secure) { should eq false }
|
||||
end
|
||||
|
||||
context "with a non-admin posting" do
|
||||
subject { FactoryBot.create(:post, non_admin_post_attributes) }
|
||||
subject { FactoryBot.create(:post, non_admin_post_attributes) }
|
||||
its(:secure) { should be_nil }
|
||||
end
|
||||
|
||||
context "with no user posting" do
|
||||
subject { FactoryBot.create(:post, post_attributes) }
|
||||
subject { FactoryBot.create(:post, post_attributes) }
|
||||
its(:secure) { should be_nil }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ describe "an instance generated by a factory that inherits from another factory"
|
|||
|
||||
FactoryBot.define do
|
||||
factory :user do
|
||||
name { "John" }
|
||||
name { "John" }
|
||||
email { "#{name.downcase}@example.com" }
|
||||
login { email }
|
||||
|
||||
|
@ -18,20 +18,20 @@ describe "an instance generated by a factory that inherits from another factory"
|
|||
end
|
||||
|
||||
describe "the parent class" do
|
||||
subject { FactoryBot.create(:user) }
|
||||
it { should_not be_admin }
|
||||
its(:name) { should eq "John" }
|
||||
subject { FactoryBot.create(:user) }
|
||||
it { should_not be_admin }
|
||||
its(:name) { should eq "John" }
|
||||
its(:email) { should eq "john@example.com" }
|
||||
its(:login) { should eq "john@example.com" }
|
||||
end
|
||||
|
||||
describe "the child class redefining parent's attributes" do
|
||||
subject { FactoryBot.create(:admin) }
|
||||
it { should be_kind_of(User) }
|
||||
it { should be_admin }
|
||||
its(:name) { should eq "admin" }
|
||||
its(:email) { should eq "admin@example.com" }
|
||||
its(:login) { should eq "admin@example.com" }
|
||||
subject { FactoryBot.create(:admin) }
|
||||
it { should be_kind_of(User) }
|
||||
it { should be_admin }
|
||||
its(:name) { should eq "admin" }
|
||||
its(:email) { should eq "admin@example.com" }
|
||||
its(:login) { should eq "admin@example.com" }
|
||||
its(:upper_email) { should eq "ADMIN@EXAMPLE.COM" }
|
||||
end
|
||||
end
|
||||
|
@ -42,7 +42,7 @@ describe "nested factories with different parents" do
|
|||
|
||||
FactoryBot.define do
|
||||
factory :user do
|
||||
name { "Basic User" }
|
||||
name { "Basic User" }
|
||||
|
||||
factory :male_user do
|
||||
name { "John Doe" }
|
||||
|
|
|
@ -12,8 +12,8 @@ describe "setting private attributes" do
|
|||
end
|
||||
end
|
||||
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot.build(:user)
|
||||
end.to raise_error NoMethodError, /foo=/
|
||||
}.to raise_error NoMethodError, /foo=/
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,8 +11,8 @@ describe "sequences" do
|
|||
first_value = generate(:email)
|
||||
another_value = generate(:email)
|
||||
|
||||
expect(first_value).to match /^somebody\d+@example\.com$/
|
||||
expect(another_value).to match /^somebody\d+@example\.com$/
|
||||
expect(first_value).to match(/^somebody\d+@example\.com$/)
|
||||
expect(another_value).to match(/^somebody\d+@example\.com$/)
|
||||
expect(first_value).not_to eq another_value
|
||||
end
|
||||
|
||||
|
@ -34,9 +34,9 @@ describe "sequences" do
|
|||
sequence(:size, aliases: [:count, :length]) { |n| "called-#{n}" }
|
||||
end
|
||||
|
||||
first_value = generate(:size)
|
||||
first_value = generate(:size)
|
||||
second_value = generate(:count)
|
||||
third_value = generate(:length)
|
||||
third_value = generate(:length)
|
||||
|
||||
expect(first_value).to eq "called-1"
|
||||
expect(second_value).to eq "called-2"
|
||||
|
@ -48,9 +48,9 @@ describe "sequences" do
|
|||
sequence(:size, "a", aliases: [:count, :length]) { |n| "called-#{n}" }
|
||||
end
|
||||
|
||||
first_value = generate(:size)
|
||||
first_value = generate(:size)
|
||||
second_value = generate(:count)
|
||||
third_value = generate(:length)
|
||||
third_value = generate(:length)
|
||||
|
||||
expect(first_value).to eq "called-a"
|
||||
expect(second_value).to eq "called-b"
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
describe "an instance generated by a factory with multiple traits" do
|
||||
before do
|
||||
define_model("User",
|
||||
name: :string,
|
||||
admin: :boolean,
|
||||
gender: :string,
|
||||
email: :string,
|
||||
date_of_birth: :date,
|
||||
great: :string)
|
||||
name: :string,
|
||||
admin: :boolean,
|
||||
gender: :string,
|
||||
email: :string,
|
||||
date_of_birth: :date,
|
||||
great: :string)
|
||||
|
||||
FactoryBot.define do
|
||||
factory :user_without_admin_scoping, class: User do
|
||||
|
@ -33,12 +33,12 @@ describe "an instance generated by a factory with multiple traits" do
|
|||
end
|
||||
|
||||
trait :male do
|
||||
name { "Joe" }
|
||||
name { "Joe" }
|
||||
gender { "Male" }
|
||||
end
|
||||
|
||||
trait :female do
|
||||
name { "Jane" }
|
||||
name { "Jane" }
|
||||
gender { "Female" }
|
||||
end
|
||||
|
||||
|
@ -77,7 +77,7 @@ describe "an instance generated by a factory with multiple traits" do
|
|||
factory :female_admin_judy, traits: [:admin]
|
||||
end
|
||||
|
||||
factory :female_admin, traits: [:female, :admin]
|
||||
factory :female_admin, traits: [:female, :admin]
|
||||
factory :female_after_male_admin, traits: [:male, :female, :admin]
|
||||
factory :male_after_female_admin, traits: [:female, :male, :admin]
|
||||
end
|
||||
|
@ -93,97 +93,97 @@ describe "an instance generated by a factory with multiple traits" do
|
|||
end
|
||||
|
||||
context "the parent class" do
|
||||
subject { FactoryBot.create(:user) }
|
||||
its(:name) { should eq "John" }
|
||||
subject { FactoryBot.create(:user) }
|
||||
its(:name) { should eq "John" }
|
||||
its(:gender) { should be_nil }
|
||||
it { should_not be_admin }
|
||||
it { should_not be_admin }
|
||||
end
|
||||
|
||||
context "the child class with one trait" do
|
||||
subject { FactoryBot.create(:admin) }
|
||||
its(:name) { should eq "John" }
|
||||
subject { FactoryBot.create(:admin) }
|
||||
its(:name) { should eq "John" }
|
||||
its(:gender) { should be_nil }
|
||||
it { should be_admin }
|
||||
it { should be_admin }
|
||||
end
|
||||
|
||||
context "the other child class with one trait" do
|
||||
subject { FactoryBot.create(:female) }
|
||||
its(:name) { should eq "Jane" }
|
||||
subject { FactoryBot.create(:female) }
|
||||
its(:name) { should eq "Jane" }
|
||||
its(:gender) { should eq "Female" }
|
||||
it { should_not be_admin }
|
||||
it { should_not be_admin }
|
||||
end
|
||||
|
||||
context "the child with multiple traits" do
|
||||
subject { FactoryBot.create(:female_admin) }
|
||||
its(:name) { should eq "Jane" }
|
||||
subject { FactoryBot.create(:female_admin) }
|
||||
its(:name) { should eq "Jane" }
|
||||
its(:gender) { should eq "Female" }
|
||||
it { should be_admin }
|
||||
it { should be_admin }
|
||||
end
|
||||
|
||||
context "the child with multiple traits and overridden attributes" do
|
||||
subject { FactoryBot.create(:female_admin, name: "Jill", gender: nil) }
|
||||
its(:name) { should eq "Jill" }
|
||||
subject { FactoryBot.create(:female_admin, name: "Jill", gender: nil) }
|
||||
its(:name) { should eq "Jill" }
|
||||
its(:gender) { should be_nil }
|
||||
it { should be_admin }
|
||||
it { should be_admin }
|
||||
end
|
||||
|
||||
context "the child with multiple traits who override the same attribute" do
|
||||
context "when the male assigns name after female" do
|
||||
subject { FactoryBot.create(:male_after_female_admin) }
|
||||
its(:name) { should eq "Joe" }
|
||||
subject { FactoryBot.create(:male_after_female_admin) }
|
||||
its(:name) { should eq "Joe" }
|
||||
its(:gender) { should eq "Male" }
|
||||
it { should be_admin }
|
||||
it { should be_admin }
|
||||
end
|
||||
|
||||
context "when the female assigns name after male" do
|
||||
subject { FactoryBot.create(:female_after_male_admin) }
|
||||
its(:name) { should eq "Jane" }
|
||||
subject { FactoryBot.create(:female_after_male_admin) }
|
||||
its(:name) { should eq "Jane" }
|
||||
its(:gender) { should eq "Female" }
|
||||
it { should be_admin }
|
||||
it { should be_admin }
|
||||
end
|
||||
end
|
||||
|
||||
context "child class with scoped trait and inherited trait" do
|
||||
subject { FactoryBot.create(:female_admin_judy) }
|
||||
its(:name) { should eq "Judy" }
|
||||
subject { FactoryBot.create(:female_admin_judy) }
|
||||
its(:name) { should eq "Judy" }
|
||||
its(:gender) { should eq "Female" }
|
||||
it { should be_admin }
|
||||
it { should be_admin }
|
||||
end
|
||||
|
||||
context "factory using global trait" do
|
||||
subject { FactoryBot.create(:user_with_email) }
|
||||
its(:name) { should eq "Bill" }
|
||||
subject { FactoryBot.create(:user_with_email) }
|
||||
its(:name) { should eq "Bill" }
|
||||
its(:email) { should eq "Bill@example.com" }
|
||||
end
|
||||
|
||||
context "factory created with alternate syntax for specifying trait" do
|
||||
subject { FactoryBot.create(:male_user) }
|
||||
subject { FactoryBot.create(:male_user) }
|
||||
its(:gender) { should eq "Male" }
|
||||
|
||||
context "where trait name and attribute are the same" do
|
||||
subject { FactoryBot.create(:great_user) }
|
||||
subject { FactoryBot.create(:great_user) }
|
||||
its(:great) { should eq "GREAT!!!" }
|
||||
end
|
||||
|
||||
context "where trait name and attribute are the same and attribute is overridden" do
|
||||
subject { FactoryBot.create(:great_user, great: "SORT OF!!!") }
|
||||
subject { FactoryBot.create(:great_user, great: "SORT OF!!!") }
|
||||
its(:great) { should eq "SORT OF!!!" }
|
||||
end
|
||||
end
|
||||
|
||||
context "factory with trait defined multiple times" do
|
||||
subject { FactoryBot.create(:great_user) }
|
||||
subject { FactoryBot.create(:great_user) }
|
||||
its(:great) { should eq "GREAT!!!" }
|
||||
|
||||
context "child factory redefining trait" do
|
||||
subject { FactoryBot.create(:even_greater_user) }
|
||||
subject { FactoryBot.create(:even_greater_user) }
|
||||
its(:great) { should eq "EVEN GREATER!!!" }
|
||||
end
|
||||
end
|
||||
|
||||
context "child factory created where trait attributes are inherited" do
|
||||
subject { FactoryBot.create(:child_male_user) }
|
||||
its(:gender) { should eq "Male" }
|
||||
subject { FactoryBot.create(:child_male_user) }
|
||||
its(:gender) { should eq "Male" }
|
||||
its(:date_of_birth) { should eq Date.parse("1/1/2000") }
|
||||
end
|
||||
|
||||
|
@ -191,13 +191,13 @@ describe "an instance generated by a factory with multiple traits" do
|
|||
subject { FactoryBot.create(:user_without_admin_scoping) }
|
||||
|
||||
it "raises an error" do
|
||||
expect { subject }.
|
||||
to raise_error(KeyError, "Trait not registered: \"admin_trait\"")
|
||||
expect { subject }
|
||||
.to raise_error(KeyError, "Trait not registered: \"admin_trait\"")
|
||||
end
|
||||
end
|
||||
|
||||
context "child factory using grandparents' trait" do
|
||||
subject { FactoryBot.create(:female_great_user) }
|
||||
subject { FactoryBot.create(:female_great_user) }
|
||||
its(:great) { should eq "GREAT!!!" }
|
||||
end
|
||||
end
|
||||
|
@ -300,8 +300,8 @@ describe "looking up traits that don't exist" do
|
|||
factory :user
|
||||
end
|
||||
|
||||
expect { FactoryBot.build(:user, double("not a trait")) }.
|
||||
to raise_error(KeyError)
|
||||
expect { FactoryBot.build(:user, double("not a trait")) }
|
||||
.to raise_error(KeyError)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -332,12 +332,12 @@ describe "traits with callbacks" do
|
|||
end
|
||||
|
||||
context "when the factory has a trait passed via arguments" do
|
||||
subject { FactoryBot.create(:caps_user) }
|
||||
subject { FactoryBot.create(:caps_user) }
|
||||
its(:name) { should eq "JOHN" }
|
||||
end
|
||||
|
||||
context "when the factory has an implicit trait" do
|
||||
subject { FactoryBot.create(:caps_user_implicit_trait) }
|
||||
subject { FactoryBot.create(:caps_user_implicit_trait) }
|
||||
its(:name) { should eq "JOHN" }
|
||||
end
|
||||
|
||||
|
@ -369,7 +369,7 @@ describe "traits added via strategy" do
|
|||
subject { FactoryBot.create(:user, :admin, :great, name: "Joe") }
|
||||
|
||||
its(:admin) { should be true }
|
||||
its(:name) { should eq "JOE" }
|
||||
its(:name) { should eq "JOE" }
|
||||
|
||||
it "doesn't modify the user factory" do
|
||||
subject
|
||||
|
@ -382,21 +382,21 @@ describe "traits added via strategy" do
|
|||
subject { FactoryBot.build(:user, :admin, :great, name: "Joe") }
|
||||
|
||||
its(:admin) { should be true }
|
||||
its(:name) { should eq "Joe" }
|
||||
its(:name) { should eq "Joe" }
|
||||
end
|
||||
|
||||
context "adding traits in attributes_for" do
|
||||
subject { FactoryBot.attributes_for(:user, :admin, :great) }
|
||||
|
||||
its([:admin]) { should be true }
|
||||
its([:name]) { should eq "John" }
|
||||
its([:name]) { should eq "John" }
|
||||
end
|
||||
|
||||
context "adding traits in build_stubbed" do
|
||||
subject { FactoryBot.build_stubbed(:user, :admin, :great, name: "Jack") }
|
||||
|
||||
its(:admin) { should be true }
|
||||
its(:name) { should eq "Jack" }
|
||||
its(:name) { should eq "Jack" }
|
||||
end
|
||||
|
||||
context "adding traits in create_list" do
|
||||
|
@ -443,9 +443,9 @@ describe "traits and dynamic attributes that are applied simultaneously" do
|
|||
end
|
||||
end
|
||||
|
||||
subject { FactoryBot.build(:user) }
|
||||
its(:name) { should eq "John" }
|
||||
its(:email) { should eq "John@example.com" }
|
||||
subject { FactoryBot.build(:user) }
|
||||
its(:name) { should eq "John" }
|
||||
its(:email) { should eq "John@example.com" }
|
||||
its(:combined) { should eq "John <John@example.com>" }
|
||||
end
|
||||
|
||||
|
@ -872,7 +872,7 @@ describe "when a self-referential trait is defined" do
|
|||
|
||||
expect { FactoryBot.build(:user, :admin) }.to raise_error(
|
||||
FactoryBot::TraitDefinitionError,
|
||||
"Self-referencing trait 'admin'",
|
||||
"Self-referencing trait 'admin'"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -889,7 +889,7 @@ describe "when a self-referential trait is defined" do
|
|||
|
||||
expect { FactoryBot.build(:user, :admin) }.to raise_error(
|
||||
FactoryBot::TraitDefinitionError,
|
||||
"Self-referencing trait 'admin'",
|
||||
"Self-referencing trait 'admin'"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,12 +7,12 @@ describe "transient attributes" do
|
|||
|
||||
factory :user do
|
||||
transient do
|
||||
four { 2 + 2 }
|
||||
four { 2 + 2 }
|
||||
rockstar { true }
|
||||
upcased { false }
|
||||
upcased { false }
|
||||
end
|
||||
|
||||
name { "#{FactoryBot.generate(:name)}#{' - Rockstar' if rockstar}" }
|
||||
name { "#{FactoryBot.generate(:name)}#{" - Rockstar" if rockstar}" }
|
||||
email { "#{name.downcase}#{four}@example.com" }
|
||||
|
||||
after(:create) do |user, evaluator|
|
||||
|
@ -27,15 +27,15 @@ describe "transient attributes" do
|
|||
it { should_not have_key(:four) }
|
||||
it { should_not have_key(:rockstar) }
|
||||
it { should_not have_key(:upcased) }
|
||||
it { should have_key(:name) }
|
||||
it { should have_key(:email) }
|
||||
it { should have_key(:name) }
|
||||
it { should have_key(:email) }
|
||||
end
|
||||
|
||||
context "with a transient variable assigned" do
|
||||
let(:rockstar) { FactoryBot.create(:user, rockstar: true, four: "1234") }
|
||||
let(:rockstar) { FactoryBot.create(:user, rockstar: true, four: "1234") }
|
||||
let(:rockstar_with_name) { FactoryBot.create(:user, name: "Jane Doe", rockstar: true) }
|
||||
let(:upcased_rockstar) { FactoryBot.create(:user, rockstar: true, upcased: true) }
|
||||
let(:groupie) { FactoryBot.create(:user, rockstar: false) }
|
||||
let(:upcased_rockstar) { FactoryBot.create(:user, rockstar: true, upcased: true) }
|
||||
let(:groupie) { FactoryBot.create(:user, rockstar: false) }
|
||||
|
||||
it "generates the correct attributes on a rockstar" do
|
||||
expect(rockstar.name).to eq "John 1 - Rockstar"
|
||||
|
@ -108,7 +108,7 @@ describe "assigning values from a transient attribute" do
|
|||
foo { Foo.new("id-of-foo", "name-of-foo") }
|
||||
end
|
||||
|
||||
foo_id { foo.id }
|
||||
foo_id { foo.id }
|
||||
foo_name { foo.name }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
describe FactoryBot::Attribute::Association do
|
||||
let(:name) { :author }
|
||||
let(:factory) { :user }
|
||||
let(:overrides) { { first_name: "John" } }
|
||||
let(:name) { :author }
|
||||
let(:factory) { :user }
|
||||
let(:overrides) { {first_name: "John"} }
|
||||
let(:association) { double("association") }
|
||||
|
||||
subject { FactoryBot::Attribute::Association.new(name, factory, overrides) }
|
||||
|
||||
module MissingMethods
|
||||
def association(*args); end
|
||||
def association(*args)
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
|
@ -15,11 +16,11 @@ describe FactoryBot::Attribute::Association do
|
|||
# Ususually this is determined via '#method_missing'
|
||||
subject.extend(MissingMethods)
|
||||
|
||||
allow(subject).
|
||||
to receive(:association).with(any_args).and_return association
|
||||
allow(subject)
|
||||
.to receive(:association).with(any_args).and_return association
|
||||
end
|
||||
|
||||
it { should be_association }
|
||||
it { should be_association }
|
||||
its(:name) { should eq name }
|
||||
|
||||
it "builds the association when calling the proc" do
|
||||
|
@ -33,6 +34,6 @@ describe FactoryBot::Attribute::Association do
|
|||
end
|
||||
|
||||
describe FactoryBot::Attribute::Association, "with a string name" do
|
||||
subject { FactoryBot::Attribute::Association.new("name", :user, {}) }
|
||||
subject { FactoryBot::Attribute::Association.new("name", :user, {}) }
|
||||
its(:name) { should eq :name }
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
describe FactoryBot::Attribute::Dynamic do
|
||||
let(:name) { :first_name }
|
||||
let(:name) { :first_name }
|
||||
let(:block) { -> {} }
|
||||
|
||||
subject { FactoryBot::Attribute::Dynamic.new(name, false, block) }
|
||||
|
@ -23,11 +23,12 @@ describe FactoryBot::Attribute::Dynamic do
|
|||
end
|
||||
|
||||
context "with a block referencing an attribute on the attribute" do
|
||||
let(:block) { -> { attribute_defined_on_attribute } }
|
||||
let(:block) { -> { attribute_defined_on_attribute } }
|
||||
let(:result) { "other attribute value" }
|
||||
|
||||
module MissingMethods
|
||||
def attribute_defined_on_attribute(*args); end
|
||||
def attribute_defined_on_attribute(*args)
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
|
@ -35,8 +36,8 @@ describe FactoryBot::Attribute::Dynamic do
|
|||
# be mocked. Ususually this is determined via '#method_missing'
|
||||
subject.extend(MissingMethods)
|
||||
|
||||
allow(subject).
|
||||
to receive(:attribute_defined_on_attribute).and_return result
|
||||
allow(subject)
|
||||
.to receive(:attribute_defined_on_attribute).and_return result
|
||||
end
|
||||
|
||||
it "evaluates the attribute from the attribute" do
|
||||
|
@ -48,7 +49,7 @@ describe FactoryBot::Attribute::Dynamic do
|
|||
let(:block) do
|
||||
-> do
|
||||
FactoryBot::Internal.register_sequence(
|
||||
FactoryBot::Sequence.new(:email, 1) { |n| "foo#{n}" },
|
||||
FactoryBot::Sequence.new(:email, 1) { |n| "foo#{n}" }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -60,6 +61,6 @@ describe FactoryBot::Attribute::Dynamic do
|
|||
end
|
||||
|
||||
describe FactoryBot::Attribute::Dynamic, "with a string name" do
|
||||
subject { FactoryBot::Attribute::Dynamic.new("name", false, -> {}) }
|
||||
subject { FactoryBot::Attribute::Dynamic.new("name", false, -> {}) }
|
||||
its(:name) { should eq :name }
|
||||
end
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
describe FactoryBot::Attribute::Sequence do
|
||||
let(:sequence_name) { :name }
|
||||
let(:name) { :first_name }
|
||||
let(:sequence) { FactoryBot::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } }
|
||||
let(:name) { :first_name }
|
||||
let(:sequence) { FactoryBot::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } }
|
||||
|
||||
subject { FactoryBot::Attribute::Sequence.new(name, sequence_name, false) }
|
||||
before { FactoryBot::Internal.register_sequence(sequence) }
|
||||
before { FactoryBot::Internal.register_sequence(sequence) }
|
||||
|
||||
its(:name) { should eq name }
|
||||
|
||||
|
|
|
@ -30,11 +30,11 @@ describe FactoryBot::AttributeList, "#define_attribute" do
|
|||
attribute = double(:attribute, name: :attribute_name)
|
||||
list = FactoryBot::AttributeList.new
|
||||
|
||||
expect do
|
||||
expect {
|
||||
2.times { list.define_attribute(attribute) }
|
||||
end.to raise_error(
|
||||
}.to raise_error(
|
||||
FactoryBot::AttributeDefinitionError,
|
||||
"Attribute already defined: attribute_name",
|
||||
"Attribute already defined: attribute_name"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -44,11 +44,11 @@ describe FactoryBot::AttributeList, "#define_attribute with a named attribute li
|
|||
association_with_same_name = FactoryBot::Attribute::Association.new(:author, :author, {})
|
||||
list = FactoryBot::AttributeList.new(:author)
|
||||
|
||||
expect do
|
||||
expect {
|
||||
list.define_attribute(association_with_same_name)
|
||||
end.to raise_error(
|
||||
}.to raise_error(
|
||||
FactoryBot::AssociationDefinitionError,
|
||||
"Self-referencing association 'author' in 'author'",
|
||||
"Self-referencing association 'author' in 'author'"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -81,7 +81,7 @@ describe FactoryBot::AttributeList, "#associations" do
|
|||
email_attribute = FactoryBot::Attribute::Dynamic.new(
|
||||
:email,
|
||||
false,
|
||||
->(u) { "#{u.full_name}@example.com" },
|
||||
->(u) { "#{u.full_name}@example.com" }
|
||||
)
|
||||
author_attribute = FactoryBot::Attribute::Association.new(:author, :user, {})
|
||||
profile_attribute = FactoryBot::Attribute::Association.new(:profile, :profile, {})
|
||||
|
@ -105,7 +105,7 @@ describe FactoryBot::AttributeList, "filter based on ignored attributes" do
|
|||
it "filters #ignored attributes" do
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:comments_count),
|
||||
build_non_ignored_attribute(:email),
|
||||
build_non_ignored_attribute(:email)
|
||||
)
|
||||
|
||||
expect(list.ignored.names).to eq [:comments_count]
|
||||
|
@ -114,7 +114,7 @@ describe FactoryBot::AttributeList, "filter based on ignored attributes" do
|
|||
it "filters #non_ignored attributes" do
|
||||
list = build_attribute_list(
|
||||
build_ignored_attribute(:comments_count),
|
||||
build_non_ignored_attribute(:email),
|
||||
build_non_ignored_attribute(:email)
|
||||
)
|
||||
|
||||
expect(list.non_ignored.names).to eq [:email]
|
||||
|
@ -140,7 +140,7 @@ describe FactoryBot::AttributeList, "generating names" do
|
|||
list = build_attribute_list(
|
||||
build_ignored_attribute(:comments_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
build_association(:avatar)
|
||||
)
|
||||
|
||||
expect(list.names).to eq [:comments_count, :last_name, :avatar]
|
||||
|
@ -150,7 +150,7 @@ describe FactoryBot::AttributeList, "generating names" do
|
|||
list = build_attribute_list(
|
||||
build_ignored_attribute(:posts_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
build_association(:avatar)
|
||||
)
|
||||
|
||||
expect(list.ignored.names).to eq [:posts_count]
|
||||
|
@ -160,7 +160,7 @@ describe FactoryBot::AttributeList, "generating names" do
|
|||
list = build_attribute_list(
|
||||
build_ignored_attribute(:posts_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
build_association(:avatar)
|
||||
)
|
||||
|
||||
expect(list.non_ignored.names).to eq [:last_name, :avatar]
|
||||
|
@ -170,7 +170,7 @@ describe FactoryBot::AttributeList, "generating names" do
|
|||
list = build_attribute_list(
|
||||
build_ignored_attribute(:posts_count),
|
||||
build_non_ignored_attribute(:last_name),
|
||||
build_association(:avatar),
|
||||
build_association(:avatar)
|
||||
)
|
||||
|
||||
expect(list.associations.names).to eq [:avatar]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
describe FactoryBot::Decorator::AttributeHash do
|
||||
describe "#attributes" do
|
||||
it "returns a hash of attributes" do
|
||||
attributes = { attribute_1: :value, attribute_2: :value }
|
||||
attributes = {attribute_1: :value, attribute_2: :value}
|
||||
component = double(:component, attributes)
|
||||
|
||||
decorator = described_class.new(component, [:attribute_1, :attribute_2])
|
||||
|
@ -11,7 +11,7 @@ describe FactoryBot::Decorator::AttributeHash do
|
|||
|
||||
context "with an attribute called 'attributes'" do
|
||||
it "does not call itself recursively" do
|
||||
attributes = { attributes: :value }
|
||||
attributes = {attributes: :value}
|
||||
component = double(:component, attributes)
|
||||
|
||||
decorator = described_class.new(component, [:attributes])
|
||||
|
|
|
@ -5,8 +5,8 @@ describe FactoryBot::DefinitionProxy, "#add_attribute" do
|
|||
attribute_value = -> { "dynamic attribute" }
|
||||
proxy.add_attribute(:attribute_name, &attribute_value)
|
||||
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name).
|
||||
with_value(attribute_value)
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name)
|
||||
.with_value(attribute_value)
|
||||
end
|
||||
|
||||
it "declares a dynamic attribute on the factory when the proxy ignores attributes" do
|
||||
|
@ -14,9 +14,9 @@ describe FactoryBot::DefinitionProxy, "#add_attribute" do
|
|||
proxy = FactoryBot::DefinitionProxy.new(definition, true)
|
||||
attribute_value = -> { "dynamic attribute" }
|
||||
proxy.add_attribute(:attribute_name, &attribute_value)
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name).
|
||||
ignored.
|
||||
with_value(attribute_value)
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name)
|
||||
.ignored
|
||||
.with_value(attribute_value)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -29,9 +29,9 @@ describe FactoryBot::DefinitionProxy, "#transient" do
|
|||
add_attribute(:attribute_name, &attribute_value)
|
||||
end
|
||||
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name).
|
||||
ignored.
|
||||
with_value(attribute_value)
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name)
|
||||
.ignored
|
||||
.with_value(attribute_value)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -49,8 +49,8 @@ describe FactoryBot::DefinitionProxy, "#method_missing" do
|
|||
proxy = FactoryBot::DefinitionProxy.new(definition)
|
||||
proxy.author factory: :user
|
||||
|
||||
expect(definition).to have_association_declaration(:author).
|
||||
with_options(factory: :user)
|
||||
expect(definition).to have_association_declaration(:author)
|
||||
.with_options(factory: :user)
|
||||
end
|
||||
|
||||
it "declares a dynamic attribute when called with a block" do
|
||||
|
@ -59,8 +59,8 @@ describe FactoryBot::DefinitionProxy, "#method_missing" do
|
|||
attribute_value = -> { "dynamic attribute" }
|
||||
proxy.attribute_name(&attribute_value)
|
||||
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name).
|
||||
with_value(attribute_value)
|
||||
expect(definition).to have_dynamic_declaration(:attribute_name)
|
||||
.with_value(attribute_value)
|
||||
end
|
||||
|
||||
it "raises a NoMethodError when called with a static-attribute-like argument" do
|
||||
|
@ -71,7 +71,7 @@ describe FactoryBot::DefinitionProxy, "#method_missing" do
|
|||
expect(invalid_call).to raise_error(
|
||||
NoMethodError,
|
||||
"undefined method 'static_attributes_are_gone' in 'broken' factory\n" \
|
||||
"Did you mean? 'static_attributes_are_gone { \"true\" }'\n",
|
||||
"Did you mean? 'static_attributes_are_gone { \"true\" }'\n"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -98,18 +98,18 @@ describe FactoryBot::DefinitionProxy, "#sequence" do
|
|||
|
||||
proxy.sequence(:sequence, override)
|
||||
|
||||
expect(FactoryBot::Sequence).to have_received(:new).
|
||||
with(:sequence, override)
|
||||
expect(FactoryBot::Sequence).to have_received(:new)
|
||||
.with(:sequence, override)
|
||||
end
|
||||
|
||||
it "creates a new sequence with a block" do
|
||||
allow(FactoryBot::Sequence).to receive(:new).and_call_original
|
||||
sequence_block = Proc.new { |n| "user+#{n}@example.com" }
|
||||
sequence_block = proc { |n| "user+#{n}@example.com" }
|
||||
proxy = build_proxy(:factory)
|
||||
proxy.sequence(:sequence, 1, &sequence_block)
|
||||
|
||||
expect(FactoryBot::Sequence).to have_received(:new).
|
||||
with(:sequence, 1, &sequence_block)
|
||||
expect(FactoryBot::Sequence).to have_received(:new)
|
||||
.with(:sequence, 1, &sequence_block)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -129,18 +129,18 @@ describe FactoryBot::DefinitionProxy, "#association" do
|
|||
|
||||
proxy.association(:association_name, name: "Awesome")
|
||||
|
||||
expect(definition).to have_association_declaration(:association_name).
|
||||
with_options(name: "Awesome")
|
||||
expect(definition).to have_association_declaration(:association_name)
|
||||
.with_options(name: "Awesome")
|
||||
end
|
||||
|
||||
it "when passing a block raises an error" do
|
||||
definition = FactoryBot::Definition.new(:post)
|
||||
proxy = FactoryBot::DefinitionProxy.new(definition)
|
||||
|
||||
expect { proxy.association(:author) {} }.
|
||||
to raise_error(
|
||||
expect { proxy.association(:author) {} }
|
||||
.to raise_error(
|
||||
FactoryBot::AssociationDefinitionError,
|
||||
"Unexpected block passed to 'author' association in 'post' factory",
|
||||
"Unexpected block passed to 'author' association in 'post' factory"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -234,7 +234,7 @@ describe FactoryBot::DefinitionProxy, "#factory" do
|
|||
proxy = FactoryBot::DefinitionProxy.new(definition)
|
||||
proxy.factory(:child, awesome: true)
|
||||
|
||||
expect(proxy.child_factories).to include([:child, { awesome: true }, nil])
|
||||
expect(proxy.child_factories).to include([:child, {awesome: true}, nil])
|
||||
end
|
||||
|
||||
it "with a block" do
|
||||
|
@ -251,7 +251,7 @@ describe FactoryBot::DefinitionProxy, "#trait" do
|
|||
it "declares a trait" do
|
||||
definition = FactoryBot::Definition.new(:name)
|
||||
proxy = FactoryBot::DefinitionProxy.new(definition)
|
||||
male_trait = Proc.new { gender { "Male" } }
|
||||
male_trait = proc { gender { "Male" } }
|
||||
proxy.trait(:male, &male_trait)
|
||||
|
||||
expect(definition).to have_trait(:male).with_block(male_trait)
|
||||
|
@ -262,7 +262,7 @@ describe FactoryBot::DefinitionProxy, "#initialize_with" do
|
|||
it "defines the constructor on the definition" do
|
||||
definition = FactoryBot::Definition.new(:name)
|
||||
proxy = FactoryBot::DefinitionProxy.new(definition)
|
||||
constructor = Proc.new { Array.new }
|
||||
constructor = proc { [] }
|
||||
proxy.initialize_with(&constructor)
|
||||
|
||||
expect(definition.constructor).to eq constructor
|
||||
|
|
|
@ -13,7 +13,7 @@ describe FactoryBot::Decorator::DisallowsDuplicatesRegistry do
|
|||
decorator = FactoryBot::Decorator::DisallowsDuplicatesRegistry.new(registry)
|
||||
allow(registry).to receive(:registered?).and_return true
|
||||
|
||||
expect { decorator.register(:same_name, {}) }.
|
||||
to raise_error(FactoryBot::DuplicateDefinitionError, "Great thing already registered: same_name")
|
||||
expect { decorator.register(:same_name, {}) }
|
||||
.to raise_error(FactoryBot::DuplicateDefinitionError, "Great thing already registered: same_name")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -22,10 +22,10 @@ describe FactoryBot::EvaluatorClassDefiner do
|
|||
|
||||
it "only instance_execs the block once even when returning nil" do
|
||||
count = 0
|
||||
attribute = stub_attribute :attribute do
|
||||
attribute = stub_attribute(:attribute) {
|
||||
count += 1
|
||||
nil
|
||||
end
|
||||
}
|
||||
evaluator = define_evaluator(attributes: [attribute])
|
||||
|
||||
2.times { evaluator.attribute }
|
||||
|
@ -47,7 +47,7 @@ describe FactoryBot::EvaluatorClassDefiner do
|
|||
child_attributes = [stub_attribute, stub_attribute]
|
||||
child_evaluator = define_evaluator(
|
||||
attributes: child_attributes,
|
||||
parent_class: parent_evaluator_class,
|
||||
parent_class: parent_evaluator_class
|
||||
)
|
||||
|
||||
expect(child_evaluator.attribute_lists).to eq [parent_attributes, child_attributes]
|
||||
|
@ -62,7 +62,7 @@ describe FactoryBot::EvaluatorClassDefiner do
|
|||
def define_evaluator_class(arguments = {})
|
||||
evaluator_class_definer = FactoryBot::EvaluatorClassDefiner.new(
|
||||
arguments[:attributes] || [],
|
||||
arguments[:parent_class] || FactoryBot::Evaluator,
|
||||
arguments[:parent_class] || FactoryBot::Evaluator
|
||||
)
|
||||
evaluator_class_definer.evaluator_class
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
describe FactoryBot::Factory do
|
||||
it "has a factory name" do
|
||||
name = :user
|
||||
name = :user
|
||||
factory = FactoryBot::Factory.new(name)
|
||||
FactoryBot::Internal.register_factory(factory)
|
||||
|
||||
|
@ -31,7 +31,7 @@ describe FactoryBot::Factory do
|
|||
|
||||
it "includes associations from the parent factory" do
|
||||
association_on_parent = FactoryBot::Declaration::Association.new(:association_on_parent, {})
|
||||
association_on_child = FactoryBot::Declaration::Association.new(:association_on_child, {})
|
||||
association_on_child = FactoryBot::Declaration::Association.new(:association_on_child, {})
|
||||
|
||||
define_class("Post")
|
||||
factory = FactoryBot::Factory.new(:post)
|
||||
|
@ -48,7 +48,7 @@ describe FactoryBot::Factory do
|
|||
it "returns the overridden value in the generated attributes" do
|
||||
name = :name
|
||||
value = "The price is right!"
|
||||
hash = { name => value }
|
||||
hash = {name => value}
|
||||
define_class("Name")
|
||||
factory = FactoryBot::Factory.new(name)
|
||||
declaration =
|
||||
|
@ -67,7 +67,7 @@ describe FactoryBot::Factory do
|
|||
declaration =
|
||||
FactoryBot::Declaration::Dynamic.new(name, false, -> { flunk })
|
||||
factory.declare_attribute(declaration)
|
||||
hash = { name.to_s => value }
|
||||
hash = {name.to_s => value}
|
||||
result = factory.run(FactoryBot::Strategy::AttributesFor, hash)
|
||||
|
||||
expect(result[name]).to eq value
|
||||
|
@ -88,7 +88,7 @@ describe FactoryBot::Factory do
|
|||
FactoryBot.aliases << [/(.*)_alias/, '\1']
|
||||
result = factory.run(
|
||||
FactoryBot::Strategy::AttributesFor,
|
||||
test_alias: "new",
|
||||
test_alias: "new"
|
||||
)
|
||||
|
||||
expect(result[:test_alias]).to eq "new"
|
||||
|
@ -107,7 +107,7 @@ describe FactoryBot::Factory do
|
|||
FactoryBot.aliases << [/(.*)_alias/, '\1']
|
||||
result = factory.run(
|
||||
FactoryBot::Strategy::AttributesFor,
|
||||
test_alias: "new",
|
||||
test_alias: "new"
|
||||
)
|
||||
|
||||
expect(result[:test]).to be_nil
|
||||
|
@ -135,7 +135,7 @@ describe FactoryBot::Factory do
|
|||
end
|
||||
|
||||
it "creates a new factory while overriding the parent class" do
|
||||
name = :user
|
||||
name = :user
|
||||
define_class("User")
|
||||
factory = FactoryBot::Factory.new(name)
|
||||
FactoryBot::Internal.register_factory(factory)
|
||||
|
@ -296,8 +296,8 @@ describe FactoryBot::Factory, "running a factory" do
|
|||
declaration = FactoryBot::Declaration::Dynamic.new(:name, false, -> { "value" })
|
||||
strategy = double("strategy", result: "result", add_observer: true)
|
||||
define_model("User", name: :string)
|
||||
allow(FactoryBot::Declaration::Dynamic).to receive(:new).
|
||||
and_return declaration
|
||||
allow(FactoryBot::Declaration::Dynamic).to receive(:new)
|
||||
.and_return declaration
|
||||
allow(declaration).to receive(:to_attributes).and_return attributes
|
||||
allow(FactoryBot::Strategy::Build).to receive(:new).and_return strategy
|
||||
factory = FactoryBot::Factory.new(:user)
|
||||
|
|
|
@ -49,7 +49,7 @@ describe "definition loading" do
|
|||
end
|
||||
end
|
||||
|
||||
%w(spec test).each do |dir|
|
||||
%w[spec test].each do |dir|
|
||||
describe "with a factories file under #{dir}" do
|
||||
in_directory_with_files File.join(dir, "factories.rb")
|
||||
it_should_behave_like "finds definitions" do
|
||||
|
@ -66,7 +66,7 @@ describe "definition loading" do
|
|||
|
||||
describe "with several factories files under #{dir}/factories" do
|
||||
in_directory_with_files File.join(dir, "factories", "post_factory.rb"),
|
||||
File.join(dir, "factories", "person_factory.rb")
|
||||
File.join(dir, "factories", "person_factory.rb")
|
||||
it_should_behave_like "finds definitions" do
|
||||
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||
it { should load_definitions_from("#{dir}/factories/person_factory.rb") }
|
||||
|
@ -75,7 +75,7 @@ describe "definition loading" do
|
|||
|
||||
describe "with several factories files under #{dir}/factories in non-alphabetical order" do
|
||||
in_directory_with_files File.join(dir, "factories", "b.rb"),
|
||||
File.join(dir, "factories", "a.rb")
|
||||
File.join(dir, "factories", "a.rb")
|
||||
it "loads the files in the right order" do
|
||||
allow(FactoryBot).to receive(:load)
|
||||
wd = File.dirname(__FILE__)
|
||||
|
@ -91,8 +91,8 @@ describe "definition loading" do
|
|||
|
||||
describe "with nested and unnested factories files under #{dir}" do
|
||||
in_directory_with_files File.join(dir, "factories.rb"),
|
||||
File.join(dir, "factories", "post_factory.rb"),
|
||||
File.join(dir, "factories", "person_factory.rb")
|
||||
File.join(dir, "factories", "post_factory.rb"),
|
||||
File.join(dir, "factories", "person_factory.rb")
|
||||
it_should_behave_like "finds definitions" do
|
||||
it { should load_definitions_from("#{dir}/factories.rb") }
|
||||
it { should load_definitions_from("#{dir}/factories/post_factory.rb") }
|
||||
|
@ -102,7 +102,7 @@ describe "definition loading" do
|
|||
|
||||
describe "with deeply nested factory files under #{dir}" do
|
||||
in_directory_with_files File.join(dir, "factories", "subdirectory", "post_factory.rb"),
|
||||
File.join(dir, "factories", "subdirectory", "person_factory.rb")
|
||||
File.join(dir, "factories", "subdirectory", "person_factory.rb")
|
||||
it_should_behave_like "finds definitions" do
|
||||
it { should load_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
|
||||
it { should load_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
|
||||
|
|
|
@ -4,10 +4,10 @@ describe FactoryBot::Internal do
|
|||
trait = FactoryBot::Trait.new(:admin)
|
||||
configuration = FactoryBot::Internal.configuration
|
||||
|
||||
expect { FactoryBot::Internal.register_trait(trait) }.
|
||||
to change { configuration.traits.count }.
|
||||
from(0).
|
||||
to(1)
|
||||
expect { FactoryBot::Internal.register_trait(trait) }
|
||||
.to change { configuration.traits.count }
|
||||
.from(0)
|
||||
.to(1)
|
||||
end
|
||||
|
||||
it "returns the registered trait" do
|
||||
|
@ -31,10 +31,10 @@ describe FactoryBot::Internal do
|
|||
sequence = FactoryBot::Sequence.new(:email)
|
||||
configuration = FactoryBot::Internal.configuration
|
||||
|
||||
expect { FactoryBot::Internal.register_sequence(sequence) }.
|
||||
to change { configuration.sequences.count }.
|
||||
from(0).
|
||||
to(1)
|
||||
expect { FactoryBot::Internal.register_sequence(sequence) }
|
||||
.to change { configuration.sequences.count }
|
||||
.from(0)
|
||||
.to(1)
|
||||
end
|
||||
|
||||
it "returns the registered sequence" do
|
||||
|
@ -75,10 +75,10 @@ describe FactoryBot::Internal do
|
|||
factory = FactoryBot::Factory.new(:object)
|
||||
configuration = FactoryBot::Internal.configuration
|
||||
|
||||
expect { FactoryBot::Internal.register_factory(factory) }.
|
||||
to change { configuration.factories.count }.
|
||||
from(0).
|
||||
to(1)
|
||||
expect { FactoryBot::Internal.register_factory(factory) }
|
||||
.to change { configuration.factories.count }
|
||||
.from(0)
|
||||
.to(1)
|
||||
end
|
||||
|
||||
it "returns the registered factory" do
|
||||
|
@ -101,10 +101,10 @@ describe FactoryBot::Internal do
|
|||
it "registers the provided factory" do
|
||||
factory = FactoryBot::Factory.new(:object)
|
||||
configuration = FactoryBot::Internal.configuration
|
||||
expect { FactoryBot::Internal.register_factory(factory) }.
|
||||
to change { configuration.factories.count }.
|
||||
from(0).
|
||||
to(1)
|
||||
expect { FactoryBot::Internal.register_factory(factory) }
|
||||
.to change { configuration.factories.count }
|
||||
.from(0)
|
||||
.to(1)
|
||||
end
|
||||
|
||||
it "returns the registered factory" do
|
||||
|
@ -125,19 +125,19 @@ describe FactoryBot::Internal do
|
|||
it "register the provided strategy name with the class" do
|
||||
configuration = FactoryBot::Internal.configuration
|
||||
initial_strategies_count = configuration.strategies.count
|
||||
expect do
|
||||
expect {
|
||||
FactoryBot::Internal.register_strategy(:strategy_name, :strategy_class)
|
||||
end.to change { configuration.strategies.count }.
|
||||
from(initial_strategies_count).
|
||||
to(initial_strategies_count + 1)
|
||||
}.to change { configuration.strategies.count }
|
||||
.from(initial_strategies_count)
|
||||
.to(initial_strategies_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
describe ".strategy_by_name" do
|
||||
it "finds a registered strategy" do
|
||||
FactoryBot::Internal.register_strategy(:strategy_name, :strategy_class)
|
||||
expect(FactoryBot::Internal.strategy_by_name(:strategy_name)).
|
||||
to eq :strategy_class
|
||||
expect(FactoryBot::Internal.strategy_by_name(:strategy_name))
|
||||
.to eq :strategy_class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,8 +24,8 @@ describe FactoryBot::Registry do
|
|||
it "raises when an object cannot be found" do
|
||||
registry = FactoryBot::Registry.new("Great thing")
|
||||
|
||||
expect { registry.find(:object_name) }.
|
||||
to raise_error(KeyError, "Great thing not registered: \"object_name\"")
|
||||
expect { registry.find(:object_name) }
|
||||
.to raise_error(KeyError, "Great thing not registered: \"object_name\"")
|
||||
end
|
||||
|
||||
it "adds and returns the object registered" do
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
shared_examples "a sequence" do |options|
|
||||
first_value = options[:first_value]
|
||||
first_value = options[:first_value]
|
||||
second_value = options[:second_value]
|
||||
|
||||
it "has a next value equal to its first value" do
|
||||
|
@ -23,9 +23,9 @@ end
|
|||
describe FactoryBot::Sequence do
|
||||
describe "a basic sequence" do
|
||||
let(:name) { :test }
|
||||
subject { FactoryBot::Sequence.new(name) { |n| "=#{n}" } }
|
||||
subject { FactoryBot::Sequence.new(name) { |n| "=#{n}" } }
|
||||
|
||||
its(:name) { should eq name }
|
||||
its(:name) { should eq name }
|
||||
its(:names) { should eq [name] }
|
||||
|
||||
it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
|
||||
|
@ -46,9 +46,9 @@ describe FactoryBot::Sequence do
|
|||
|
||||
it "has the expected names as its names" do
|
||||
names = [:foo, :bar, :baz]
|
||||
sequence = FactoryBot::Sequence.new(names.first, aliases: names.last(2)) do
|
||||
sequence = FactoryBot::Sequence.new(names.first, aliases: names.last(2)) {
|
||||
"=#{n}"
|
||||
end
|
||||
}
|
||||
|
||||
expect(sequence.names).to eq names
|
||||
end
|
||||
|
@ -65,9 +65,9 @@ describe FactoryBot::Sequence do
|
|||
|
||||
it "has the expected names as its names" do
|
||||
names = [:foo, :bar, :baz]
|
||||
sequence = FactoryBot::Sequence.new(names.first, 3, aliases: names.last(2)) do
|
||||
sequence = FactoryBot::Sequence.new(names.first, 3, aliases: names.last(2)) {
|
||||
"=#{n}"
|
||||
end
|
||||
}
|
||||
expect(sequence.names).to eq names
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
describe FactoryBot::Strategy::AttributesFor do
|
||||
let(:result) { { name: "John Doe", gender: "Male", admin: false } }
|
||||
let(:result) { {name: "John Doe", gender: "Male", admin: false} }
|
||||
let(:evaluation) { double("evaluation", hash: result) }
|
||||
|
||||
it_should_behave_like "strategy without association support"
|
||||
|
@ -9,8 +9,8 @@ describe FactoryBot::Strategy::AttributesFor do
|
|||
end
|
||||
|
||||
it "does not run the to_create block" do
|
||||
expect do
|
||||
expect {
|
||||
subject.result(evaluation)
|
||||
end.to_not raise_error
|
||||
}.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ describe FactoryBot::Strategy::Create do
|
|||
"evaluation",
|
||||
object: nil,
|
||||
notify: nil,
|
||||
create: nil,
|
||||
create: nil
|
||||
)
|
||||
|
||||
subject.result(evaluation)
|
||||
|
|
|
@ -11,7 +11,7 @@ shared_examples "disabled persistence method" do |method_name|
|
|||
it "raises an informative error if the method is called" do
|
||||
expect { instance.send(method_name) }.to raise_error(
|
||||
RuntimeError,
|
||||
"stubbed models are not allowed to access the database - #{instance.class}##{method_name}()",
|
||||
"stubbed models are not allowed to access the database - #{instance.class}##{method_name}()"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -24,9 +24,9 @@ describe FactoryBot::Strategy::Stub do
|
|||
|
||||
context "asking for a result" do
|
||||
let(:result_instance) do
|
||||
define_class("ResultInstance") do
|
||||
define_class("ResultInstance") {
|
||||
attr_accessor :id, :created_at
|
||||
end.new
|
||||
}.new
|
||||
end
|
||||
|
||||
let(:evaluation) do
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
describe FactoryBot do
|
||||
it "finds a registered strategy" do
|
||||
FactoryBot.register_strategy(:strategy_name, :strategy_class)
|
||||
expect(FactoryBot.strategy_by_name(:strategy_name)).
|
||||
to eq :strategy_class
|
||||
expect(FactoryBot.strategy_by_name(:strategy_name))
|
||||
.to eq :strategy_class
|
||||
end
|
||||
|
||||
describe ".use_parent_strategy" do
|
||||
|
|
|
@ -39,9 +39,9 @@ module DefineConstantMacros
|
|||
end
|
||||
|
||||
def clear_generated_table(table_name)
|
||||
ActiveRecord::Base.
|
||||
connection.
|
||||
execute("DROP TABLE IF EXISTS #{table_name}")
|
||||
ActiveRecord::Base
|
||||
.connection
|
||||
.execute("DROP TABLE IF EXISTS #{table_name}")
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -57,7 +57,7 @@ RSpec.configure do |config|
|
|||
config.before(:all) do
|
||||
ActiveRecord::Base.establish_connection(
|
||||
adapter: "sqlite3",
|
||||
database: ":memory:",
|
||||
database: ":memory:"
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ module DeclarationMatchers
|
|||
def failure_message
|
||||
[
|
||||
"expected declarations to include declaration of type #{@declaration_type}",
|
||||
@options ? "with options #{options}" : nil,
|
||||
@options ? "with options #{options}" : nil
|
||||
].compact.join " "
|
||||
end
|
||||
|
||||
|
@ -56,8 +56,8 @@ module DeclarationMatchers
|
|||
|
||||
def expected_declaration
|
||||
case @declaration_type
|
||||
when :dynamic then FactoryBot::Declaration::Dynamic.new(@name, ignored?, @value)
|
||||
when :implicit then FactoryBot::Declaration::Implicit.new(@name, @factory, ignored?)
|
||||
when :dynamic then FactoryBot::Declaration::Dynamic.new(@name, ignored?, @value)
|
||||
when :implicit then FactoryBot::Declaration::Implicit.new(@name, @factory, ignored?)
|
||||
when :association
|
||||
if @options
|
||||
FactoryBot::Declaration::Association.new(@name, options)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
shared_examples_for "strategy without association support" do
|
||||
let(:factory) { double("associate_factory") }
|
||||
let(:factory) { double("associate_factory") }
|
||||
let(:attribute) { FactoryBot::Attribute::Association.new(:user, :user, {}) }
|
||||
|
||||
def association_named(name, overrides)
|
||||
|
@ -70,9 +70,9 @@ end
|
|||
|
||||
shared_examples_for "strategy with callbacks" do |*callback_names|
|
||||
let(:result_instance) do
|
||||
define_class("ResultInstance") do
|
||||
define_class("ResultInstance") {
|
||||
attr_accessor :id
|
||||
end.new
|
||||
}.new
|
||||
end
|
||||
|
||||
let(:evaluation) do
|
||||
|
|
Loading…
Reference in New Issue