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"
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -7,7 +7,7 @@ module FactoryBot
|
|||
:inline_sequences,
|
||||
:sequences,
|
||||
:strategies,
|
||||
:traits,
|
||||
:traits
|
||||
)
|
||||
|
||||
def initialize
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -19,10 +19,9 @@ 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
|
||||
|
||||
|
@ -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:
|
||||
|
|
|
@ -10,12 +10,18 @@ module FactoryBot
|
|||
delegate :defined_traits, :callbacks, :attributes, :constructor,
|
||||
: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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -13,7 +13,7 @@ module FactoryBot
|
|||
@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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -15,7 +15,7 @@ describe "declaring attributes on a Factory that are private methods on Object"
|
|||
|
||||
its(:system) { should eq false }
|
||||
its(:link) { should eq "http://example.com" }
|
||||
its(:sleep) { should eq -5 }
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -35,9 +35,9 @@ describe "modifying factories" do
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -191,8 +191,8 @@ 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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -12,7 +12,7 @@ describe "transient attributes" do
|
|||
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|
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
describe FactoryBot::Attribute::Association do
|
||||
let(:name) { :author }
|
||||
let(:factory) { :user }
|
||||
let(:overrides) { { first_name: "John" } }
|
||||
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,8 +16,8 @@ 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 }
|
||||
|
|
|
@ -27,7 +27,8 @@ describe FactoryBot::Attribute::Dynamic do
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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