Class: BoltSpec::Plans::TaskStub

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_spec/plans/mock_executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expect = false) ⇒ TaskStub

Returns a new instance of TaskStub.



41
42
43
44
45
46
47
48
49
# File 'lib/bolt_spec/plans/mock_executor.rb', line 41

def initialize(expect = false)
  @calls = 0
  @expect = expect
  @expected_calls = 1
  # invocation spec
  @invocation = {}
  # return value
  @data = { default: {} }
end

Instance Attribute Details

#invocationObject (readonly)

Returns the value of attribute invocation.



39
40
41
# File 'lib/bolt_spec/plans/mock_executor.rb', line 39

def invocation
  @invocation
end

Instance Method Details

#always_return(default_data) ⇒ Object

Set a default return value for all targets, specific targets may be overridden with return_for_targets



167
168
169
# File 'lib/bolt_spec/plans/mock_executor.rb', line 167

def always_return(default_data)
  return_for_targets(default: default_data)
end

#assert_called(taskname) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bolt_spec/plans/mock_executor.rb', line 85

def assert_called(taskname)
  satisfied = if @expect
                (@expected_calls.nil? && @calls > 0) || @calls == @expected_calls
              else
                @expected_calls.nil? || @calls <= @expected_calls
              end
  unless satisfied
    unless (times = @expected_calls)
      times = @expect ? "at least one" : "any number of"
    end
    message = "Expected #{taskname} to be called #{times} times"
    message += " with targets #{@invocation[:targets]}" if @invocation[:targets]
    message += " with parameters #{@invocations[:parameters]}" if @invocation[:parameters]
    raise message
  end
end

#be_called_times(times) ⇒ Object

limit the maximum number of times an allow stub may be called or specify how many times an expect stub must be called.



138
139
140
141
# File 'lib/bolt_spec/plans/mock_executor.rb', line 138

def be_called_times(times)
  @expected_calls = times
  self
end

#call(targets, task, arguments, options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bolt_spec/plans/mock_executor.rb', line 67

def call(targets, task, arguments, options)
  @calls += 1
  if @return_block
    # Merge arguments and options into params to match puppet function signature.
    result_set = @return_block.call(targets: targets, task: task, params: arguments.merge(options))
    unless result_set.is_a?(Bolt::ResultSet)
      raise "Return block for #{task} did not return a Bolt::ResultSet"
    end
    result_set
  else
    results = targets.map do |target|
      val = @data[target.name] || @data[:default]
      Bolt::Result.new(target, value: val)
    end
    Bolt::ResultSet.new(results)
  end
end

#error_with(error_data) ⇒ Object

Set a default error result for all targets.



172
173
174
# File 'lib/bolt_spec/plans/mock_executor.rb', line 172

def error_with(error_data)
  always_return("_error" => error_data)
end

#expect_callObject

This changes the stub from an allow to an expect which will validate that it has been called.



104
105
106
107
# File 'lib/bolt_spec/plans/mock_executor.rb', line 104

def expect_call
  @expect = true
  self
end

#matches(targets, _task, arguments, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bolt_spec/plans/mock_executor.rb', line 51

def matches(targets, _task, arguments, options)
  if @invocation[:targets] && Set.new(@invocation[:targets]) != Set.new(targets.map(&:name))
    return false
  end

  if @invocation[:arguments] && arguments != @invocation[:arguments]
    return false
  end

  if @invocation[:options] && options != @invocation[:options]
    return false
  end

  true
end

#not_be_calledObject

error if the stub is called at all.



144
145
146
147
# File 'lib/bolt_spec/plans/mock_executor.rb', line 144

def not_be_called
  @expected_calls = 0
  self
end

#return(&block) ⇒ Object



149
150
151
152
153
# File 'lib/bolt_spec/plans/mock_executor.rb', line 149

def return(&block)
  raise "Cannot set return values and return block." if @data_set
  @return_block = block
  self
end

#return_for_targets(data) ⇒ Object

Set different result values for each target



156
157
158
159
160
161
162
163
164
# File 'lib/bolt_spec/plans/mock_executor.rb', line 156

def return_for_targets(data)
  data.each do |target, result|
    raise "Mocked results must be hashes: #{target}: #{result}" unless result.is_a? Hash
  end
  raise "Cannot set return values and return block." if @return_block
  @data = data
  @data_set = true
  self
end

#with_params(params) ⇒ Object

Restricts the stub to only match invocations with certain parameters All parameters must match exactly and since arguments and options are treated differently at the executor this won’t work with some ‘_*’ options TODO: Fix handling of ‘_*’ options probably by breaking them into other helpers



129
130
131
132
133
134
# File 'lib/bolt_spec/plans/mock_executor.rb', line 129

def with_params(params)
  @invocation[:parameters] = params
  @invocation[:arguments] = params.reject { |k, _v| k.start_with?('_') }
  @invocation[:options] = params.select { |k, _v| k.start_with?('_') }
  self
end

#with_targets(targets) ⇒ Object

Restricts the stub to only match invocations with the correct targets



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bolt_spec/plans/mock_executor.rb', line 113

def with_targets(targets)
  targets = [targets] unless targets.is_a? Array
  @invocation[:targets] = targets.map do |target|
    if target.is_a? String
      target
    else
      target.name
    end
  end
  self
end