Class: EmailSpec::Matchers::HaveBodyText

Inherits:
Object
  • Object
show all
Defined in:
lib/email_spec/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ HaveBodyText

Returns a new instance of HaveBodyText.



277
278
279
280
# File 'lib/email_spec/matchers.rb', line 277

def initialize(text)
  @expected_text = text
  @extractor = EmailSpec::Extractors::DefaultPartBody
end

Instance Method Details

#descriptionObject



282
283
284
285
286
287
288
# File 'lib/email_spec/matchers.rb', line 282

def description
  if @expected_text.is_a?(String)
    "have body including #{@expected_text.inspect}"
  else
    "have body matching #{@expected_text.inspect}"
  end
end

#failure_messageObject



311
312
313
314
315
316
317
# File 'lib/email_spec/matchers.rb', line 311

def failure_message
  if @expected_text.is_a?(String)
    "expected the body to contain #{@expected_text.inspect} but was #{@given_text.inspect}"
  else
    "expected the body to match #{@expected_text.inspect}, but did not.  Actual body was: #{@given_text.inspect}"
  end
end

#failure_message_when_negatedObject Also known as: negative_failure_message



319
320
321
322
323
324
325
# File 'lib/email_spec/matchers.rb', line 319

def failure_message_when_negated
  if @expected_text.is_a?(String)
    "expected the body not to contain #{@expected_text.inspect} but was #{@given_text.inspect}"
  else
    "expected the body not to match #{@expected_text.inspect} but #{@given_text.inspect} does match it."
  end
end

#in_html_partObject



290
291
292
293
# File 'lib/email_spec/matchers.rb', line 290

def in_html_part
  @extractor = EmailSpec::Extractors::HtmlPartBody
  self
end

#in_text_partObject



295
296
297
298
# File 'lib/email_spec/matchers.rb', line 295

def in_text_part
  @extractor = EmailSpec::Extractors::TextPartBody
  self
end

#matches?(email) ⇒ Boolean

Returns:

  • (Boolean)


300
301
302
303
304
305
306
307
308
309
# File 'lib/email_spec/matchers.rb', line 300

def matches?(email)
  if @expected_text.is_a?(String)
    @given_text = @extractor.new(email).call.to_s.gsub(/\s+/, " ")
    @expected_text = @expected_text.gsub(/\s+/, " ")
    @given_text.include?(@expected_text)
  else
    @given_text = @extractor.new(email).call.to_s
    !!(@given_text =~ @expected_text)
  end
end