Class: RSpec::Chef::Matchers::ContainResource

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-chef/matchers/contain_resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, *args, &block) ⇒ ContainResource

Returns a new instance of ContainResource.



12
13
14
15
16
17
18
19
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 12

def initialize(type, *args, &block)
  @type                  = type.to_s[CONTAIN_PATTERN, 1]
  @name                  = args.shift
  @params                = args.shift || {}
  @expected_attributes   = {}
  @unexpected_attributes = []
  @errors                = []
end

Instance Method Details

#descriptionObject



53
54
55
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 53

def description
  %Q{include Resource[#{@type} @name="#{@name}"]}
end

#errorsObject



61
62
63
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 61

def errors
  @errors.empty? ? "" : " with #{@errors.join(', and ')}"
end

#failure_message_for_shouldObject



57
58
59
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 57

def failure_message_for_should
  %Q{expected that the recipe would #{description}#{errors}}
end

#matches?(recipe) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 21

def matches?(recipe)
  lookup = @type
  lookup << "[#{@name.to_s}]" if @name

  resource = recipe.resources(lookup)
  return false unless resource

  matches = true
  @params.each do |key, value|
    unless (real_value = resource.params[key.to_sym]) == value
      @errors << "#{key} expected to be #{value} but it is #{real_value}"
      matches = false
    end
  end

  @expected_attributes.each do |key, value|
    unless (real_value = resource.send(key.to_sym)) == value
      @errors << "#{key} expected to be #{value} but it is #{real_value}"
      matches = false
    end
  end

  @unexpected_attributes.flatten.each do |attr|
    unless (real_value = resource.send(attr.to_sym)) == nil
      @errors << "#{attr} expected to be nil but it is #{real_value}"
      matches = false
    end
  end

  matches
end

#with(attribute, value) ⇒ Object



65
66
67
68
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 65

def with(attribute, value)
  @expected_attributes[attribute] = value
  self
end

#without(*attributes) ⇒ Object



70
71
72
73
# File 'lib/rspec-chef/matchers/contain_resource.rb', line 70

def without(*attributes)
  @unexpected_attributes << attributes
  self
end