1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/support/matchers/declaration.rb
Daniel Colson 5f1a1de114 Run standardrb
This commit applies the changes from running `standardrb --fix`
2020-06-10 17:11:39 -04:00

78 lines
1.6 KiB
Ruby

module DeclarationMatchers
def have_dynamic_declaration(name)
DeclarationMatcher.new(:dynamic).named(name)
end
def have_association_declaration(name)
DeclarationMatcher.new(:association).named(name)
end
def have_implicit_declaration(name)
DeclarationMatcher.new(:implicit).named(name)
end
class DeclarationMatcher
def initialize(declaration_type)
@declaration_type = declaration_type
end
def matches?(subject)
subject.declarations.include?(expected_declaration)
end
def named(name)
@name = name
self
end
def ignored
@ignored = true
self
end
def with_value(value)
@value = value
self
end
def with_factory(factory)
@factory = factory
self
end
def with_options(options)
@options = options
self
end
def failure_message
[
"expected declarations to include declaration of type #{@declaration_type}",
@options ? "with options #{options}" : nil
].compact.join " "
end
private
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 :association
if @options
FactoryBot::Declaration::Association.new(@name, options)
else
FactoryBot::Declaration::Association.new(@name)
end
end
end
def ignored?
!!@ignored
end
def options
@options || {}
end
end
end