Class: ModelTimeline::RSpec::Matchers::HaveTimelineEntriesMatcher Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

RSpec matcher to check if a model has timeline entries

Instance Method Summary collapse

Constructor Details

#initialize(expected_count, association_name) ⇒ HaveTimelineEntriesMatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the matcher

Parameters:

  • expected_count (Integer, nil)

    The expected number of timeline entries



99
100
101
102
# File 'lib/model_timeline/rspec/matchers.rb', line 99

def initialize(expected_count, association_name)
  @expected_count = expected_count
  @association_name = association_name
end

Instance Method Details

#failure_messageString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Message displayed when the expectation fails

Returns:

  • (String)

    A descriptive failure message



120
121
122
123
124
125
126
127
# File 'lib/model_timeline/rspec/matchers.rb', line 120

def failure_message
  if @expected_count.nil?
    "expected #{@subject} to have timeline entries, but found none"
  else
    "expected #{@subject} to have #{@expected_count} timeline entries, " \
      "but found #{@subject.public_send(@association_name).count}"
  end
end

#failure_message_when_negatedString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Message displayed when the negated expectation fails

Returns:

  • (String)

    A descriptive failure message for negated expectations



132
133
134
135
136
137
138
# File 'lib/model_timeline/rspec/matchers.rb', line 132

def failure_message_when_negated
  if @expected_count.nil?
    "expected #{@subject} not to have any timeline entries, but found #{@subject.public_send(@association_name).count}"
  else
    "expected #{@subject} not to have #{@expected_count} timeline entries, but found exactly that many"
  end
end

#matches?(subject) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the subject matches the expectations

Parameters:

  • subject (Object)

    The model to check for timeline entries

Returns:

  • (Boolean)

    True if the model has the expected number of timeline entries



108
109
110
111
112
113
114
115
# File 'lib/model_timeline/rspec/matchers.rb', line 108

def matches?(subject)
  @subject = subject
  if @expected_count.nil?
    subject.public_send(@association_name).any?
  else
    subject.public_send(@association_name).count == @expected_count
  end
end