Class: Phillumeny::FactoryBot::HaveAValidFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/phillumeny/factory_bot.rb

Overview

it { should_not have_a_valid_factory } it { should have_a_valid_factory.with_trait(:valid) }

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ HaveAValidFactory

Returns a new instance of HaveAValidFactory.



35
36
37
38
# File 'lib/phillumeny/factory_bot.rb', line 35

def initialize(name = nil)
  @name = name
  @trait = nil
end

Instance Method Details

#descriptionObject



21
22
23
24
25
# File 'lib/phillumeny/factory_bot.rb', line 21

def description
  msg = "have a valid factory '#{@name}'"
  msg << " with the trait '#{@trait}'" if @trait
  msg
end

#failure_messageObject



27
28
29
# File 'lib/phillumeny/factory_bot.rb', line 27

def failure_message
  "The '#{@name}' factory did not pass validation.\n#{@error_messages.join("\n")}"
end

#failure_message_when_negatedObject



31
32
33
# File 'lib/phillumeny/factory_bot.rb', line 31

def failure_message_when_negated
  "The '#{@name}' factory passed validation unexpectedly"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/phillumeny/factory_bot.rb', line 40

def matches?(subject)
  factory_name = @name = @name.nil? ? subject.class.to_s.underscore : @name
  object = ::FactoryBot.build(factory_name, @trait)
  @error_messages = object.errors.full_messages unless object.valid?
  object.valid?
end

#with_trait(trait) ⇒ Object



47
48
49
50
# File 'lib/phillumeny/factory_bot.rb', line 47

def with_trait(trait)
  @trait = trait
  self
end