Class: ShouldaExt::Matchers::RecordCountChangeMatcher
- Inherits:
-
Object
- Object
- ShouldaExt::Matchers::RecordCountChangeMatcher
- Defined in:
- lib/shoulda_ext/matchers/record_count_change.rb
Overview
:nodoc:
Instance Method Summary collapse
- #before ⇒ Object
- #by(expected_change) ⇒ Object
- #description ⇒ Object
- #errors ⇒ Object
- #expected_count ⇒ Object
- #failure_message ⇒ Object
- #for(klass) ⇒ Object
- #found_expected? ⇒ Boolean
- #matches? ⇒ Boolean
- #negative_failure_message ⇒ Object
Instance Method Details
#before ⇒ Object
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 |
#description ⇒ Object
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 |
#errors ⇒ Object
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_count ⇒ Object
113 114 115 |
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 113 def expected_count @previous_count + @expected_change end |
#failure_message ⇒ Object
97 98 99 |
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 97 def "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
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
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_message ⇒ Object
101 102 103 |
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 101 def "Did not expect #{errors.join("\n")}" end |