Class: ChefSpec::Matchers::NotificationsMatcher

Inherits:
Object
  • Object
show all
Includes:
Normalize
Defined in:
lib/chefspec/matchers/notifications_matcher.rb

Instance Method Summary collapse

Methods included from Normalize

#resource_name

Constructor Details

#initialize(signature) ⇒ NotificationsMatcher

Returns a new instance of NotificationsMatcher.



5
6
7
8
9
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 5

def initialize(signature)
  signature.match(/^([^\[]*)\[(.*)\]$/)
  @expected_resource_type = $1
  @expected_resource_name = $2
end

Instance Method Details

#beforeObject



48
49
50
51
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 48

def before
  @before = true
  self
end

#delayedObject



43
44
45
46
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 43

def delayed
  @delayed = true
  self
end

#descriptionObject



53
54
55
56
57
58
59
60
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 53

def description
  message = %Q{notify "#{@expected_resource_type}[#{@expected_resource_name}]"}
  message << " with action :#{@action}" if @action
  message << " immediately" if @immediately
  message << " delayed" if @delayed
  message << " before" if @before
  message
end

#failure_messageObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 62

def failure_message
  if @resource
    message = %Q{expected "#{@resource}" to notify "#{@expected_resource_type}[#{@expected_resource_name}]"}
    message << " with action :#{@action}" if @action
    message << " immediately" if @immediately
    message << " delayed" if @delayed
    message << " before" if @before
    message << ", but did not."
    message << "\n\n"
    message << "Other notifications were:\n\n#{format_notifications}"
    message << "\n "
    message
  else
    message = %Q{expected _something_ to notify "#{@expected_resource_type}[#{@expected_resource_name}]"}
    message << " with action :#{@action}" if @action
    message << " immediately" if @immediately
    message << " delayed" if @delayed
    message << " before" if @before
    message << ", but the _something_ you gave me was nil! If you are running a test like:"
    message << "\n\n"
    message << "  expect(_something_).to notify('...')"
    message << "\n\n"
    message << "Make sure that `_something_` exists, because I got nil"
    message << "\n "
    message
  end
end

#failure_message_when_negatedObject



90
91
92
93
94
95
96
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 90

def failure_message_when_negated
  if @resource
    message = %Q{expected "#{@resource}" to not notify "#{@expected_resource_type}[#{@expected_resource_name}]"}
    message << ", but it did."
    message
  end
end

#immediatelyObject



38
39
40
41
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 38

def immediately
  @immediately = true
  self
end

#matches?(resource) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 11

def matches?(resource)
  @resource = resource

  if @resource
    block = Proc.new do |notified|
      resource_name(notified.resource).to_s == @expected_resource_type &&
        (@expected_resource_name === notified.resource.identity.to_s || @expected_resource_name === notified.resource.name.to_s) &&
        matches_action?(notified)
    end

    if @immediately
      immediate_notifications.any?(&block)
    elsif @delayed
      delayed_notifications.any?(&block)
    elsif @before
      before_notifications.any?(&block)
    else
      all_notifications.any?(&block)
    end
  end
end

#to(action) ⇒ Object



33
34
35
36
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 33

def to(action)
  @action = action.to_sym
  self
end