Class: RSpec::ActiveRecord::ChangeRecord

Inherits:
RecordMatcher show all
Includes:
Matchers::BuiltIn::BaseMatcher::HashFormatting
Defined in:
lib/rspec/active_record/change_record.rb

Overview

A matcher

Instance Method Summary collapse

Methods inherited from RecordMatcher

#initialize

Methods inherited from Matcher

#supports_block_expectations?

Constructor Details

This class inherits a constructor from RSpec::ActiveRecord::RecordMatcher

Instance Method Details

#descriptionObject



46
47
48
49
50
51
# File 'lib/rspec/active_record/change_record.rb', line 46

def description
  message = "change #{format_record}"
  message += " from #{format_hash(@from)}" if @from
  message += " to #{format_hash(@to)}" if @to
  message
end

#does_not_match?(block) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec/active_record/change_record.rb', line 34

def does_not_match?(block)
  return false if @to || @from.blank?

  @from_diff = diff_unless_match(@record, @from)
  return false if @from_diff

  block.call
  @updated_record = @record.class.find(@record.id)

  @from_post_match = match_attributes?(@updated_record, @from)
end

#failure_messageObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec/active_record/change_record.rb', line 53

def failure_message
  if @from_post_match
    "expected to change #{format_record} from #{format_hash(@from)} but did not"
  elsif @to_pre_match
    "expected #{format_record} to not match #{format_hash(@to)} initially but it did"
  elsif @from_diff
    "expected #{format_record} to match #{format_hash(@from)} initially but it did not#{@from_diff}"
  elsif @to_diff
    "expected to change #{format_record} to #{format_hash(@to)} but did not#{@to_diff}"
  else
    "please specify from or to for change_record"
  end
end

#failure_message_when_negatedObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rspec/active_record/change_record.rb', line 67

def failure_message_when_negated
  if @to
    "negative matcher for change_record with to is not supported"
  elsif @from.blank?
    "negative matcher for change_record without from is not supported"
  elsif @from_diff
    "expected #{format_record} to match #{format_hash(@from)} initially but it did not#{@from_diff}"
  elsif !@from_post_match
    "expected not to change #{format_record} from #{format_hash(@from)} but it did"
  end
end

#from(attributes) ⇒ Object

Attributes of record should match these before block is executed



16
17
18
19
# File 'lib/rspec/active_record/change_record.rb', line 16

def from(attributes)
  @from = attributes
  self
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rspec/active_record/change_record.rb', line 21

def matches?(block)
  @from_diff = diff_unless_match(@record, @from)
  @to_pre_match = !does_not_match_attributes?(@record, @to)

  block.call
  @updated_record = @record.class.base_class.find(@record.id)

  @to_diff = diff_unless_match(@updated_record, @to)
  @from_post_match = !does_not_match_attributes?(@updated_record, @from)

  (@from || @to) && !@from_diff && !@to_pre_match && !@to_diff && !@from_post_match
end

#to(attributes) ⇒ Object

Attributes of record should match these after block is executed



10
11
12
13
# File 'lib/rspec/active_record/change_record.rb', line 10

def to(attributes)
  @to = attributes
  self
end