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.



33
34
35
36
# File 'lib/phillumeny/factory_bot.rb', line 33

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

Instance Method Details

#descriptionObject



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

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

#failure_messageObject



25
26
27
# File 'lib/phillumeny/factory_bot.rb', line 25

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

#failure_message_when_negatedObject



29
30
31
# File 'lib/phillumeny/factory_bot.rb', line 29

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/phillumeny/factory_bot.rb', line 38

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



45
46
47
48
# File 'lib/phillumeny/factory_bot.rb', line 45

def with_trait(trait)
  @trait = trait
  self
end