Class: ShouldaExt::Matchers::RecordCountChangeMatcher

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#beforeObject



72
73
74
75
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 72

def before
  @triggered_before = true
  @previous_count = @klass.count
end

#by(expected_change) ⇒ Object



83
84
85
86
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 83

def by(expected_change)
  @expected_change = expected_change
  self
end

#descriptionObject



93
94
95
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 93

def description
  "expect #{@klass.name}.count to change#{" by #{@expected_change} records" if @expected_change }"
end

#errorsObject



105
106
107
108
109
110
111
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 105

def errors
  @errors = []
  @errors << "Never received :before call.  Please make sure you are running this with a setup/subject block in the current context and the ContextWithMatcherBeforeHooks patch is installed" if !@triggered_before
  @errors << "#{expected_count} #{@klass.name} records, but found #{@new_count}" if !found_expected? && @expected_change
  @errors << "#{@klass.name}.count to change" if !found_expected? && !@expected_change
  @errors
end

#expected_countObject



113
114
115
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 113

def expected_count
  @previous_count + @expected_change
end

#failure_messageObject



97
98
99
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 97

def failure_message
  "Expected #{errors.join("\n")}"
end

#for(klass) ⇒ Object



77
78
79
80
81
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 77

def for(klass)
  klass = klass.to_s.classify.constantize if klass.is_a? Symbol
  @klass = klass
  self
end

#found_expected?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 117

def found_expected?
  if @expected_change
    expected_count == @new_count
  else
    @new_count != @previous_count
  end
end

#matches?Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 88

def matches?(*)
  @new_count = @klass.count
  found_expected?
end

#negative_failure_messageObject



101
102
103
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 101

def negative_failure_message
  "Did not expect #{errors.join("\n")}"
end