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

#delayedObject



41
42
43
44
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 41

def delayed
  @delayed = true
  self
end

#descriptionObject



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

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
end

#failure_message_for_shouldObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 54

def failure_message_for_should
  if @resource
    message = %Q{expected "#{resource_name(@resource)}[#{@resource.name}]" to notify "#{@expected_resource_type}[#{@expected_resource_name}]"}
    message << " with action :#{@action}" if @action
    message << " immediately" if @immediately
    message << " delayed" if @delayed
    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 << ", 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

#immediatelyObject



36
37
38
39
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 36

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
# 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)
    else
      all_notifications.any?(&block)
    end
  end
end

#to(action) ⇒ Object



31
32
33
34
# File 'lib/chefspec/matchers/notifications_matcher.rb', line 31

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