Class: BoltSpec::Plans::ActionStub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expect = false, inventory = nil) ⇒ ActionStub

Returns a new instance of ActionStub.



37
38
39
40
41
42
43
44
45
46
# File 'lib/bolt_spec/plans/action_stubs.rb', line 37

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

Instance Attribute Details

#invocationObject (readonly)

Returns the value of attribute invocation.



35
36
37
# File 'lib/bolt_spec/plans/action_stubs.rb', line 35

def invocation
  @invocation
end

Instance Method Details

#always_return(data) ⇒ Object

Set a default return value for all targets, specific targets may be overridden with return_for_targets. Follows the same rules for data as return_for_targets.



173
174
175
176
177
# File 'lib/bolt_spec/plans/action_stubs.rb', line 173

def always_return(data)
  @data[:default] = data
  @data_set = true
  self
end

#assert_called(object) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bolt_spec/plans/action_stubs.rb', line 48

def assert_called(object)
  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 #{object} to be called #{times} times"
    message += " with targets #{@invocation[:targets]}" if @invocation[:targets]
    if parameters
      # Print the parameters hash by converting it to JSON and then re-parsing.
      # This prevents issues in Bolt data types, such as Targets, from generating
      # gigantic, unreadable, data when converted to string by interpolation.
      # Targets exhibit this behavior because they have a reference to @inventory.
      # When the target is converted into a string, it converts the full Inventory
      # into a string recursively.
      parameters_str = JSON.parse(parameters.to_json)
      message += " with parameters #{parameters_str}"
    end
    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.



136
137
138
139
# File 'lib/bolt_spec/plans/action_stubs.rb', line 136

def be_called_times(times)
  @expected_calls = times
  self
end

#check_plan_result(plan_result, plan_clj) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/bolt_spec/plans/action_stubs.rb', line 110

def check_plan_result(plan_result, plan_clj)
  unless plan_result.is_a?(Bolt::PlanResult)
    raise "Return block for #{plan_clj.closure_name} did not return a Bolt::PlanResult"
  end

  plan_result
end

#check_resultset(result_set, object) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/bolt_spec/plans/action_stubs.rb', line 102

def check_resultset(result_set, object)
  unless result_set.is_a?(Bolt::ResultSet)
    raise "Return block for #{object} did not return a Bolt::ResultSet"
  end

  result_set
end

#default_for(target) ⇒ Object

Used to create a valid Bolt::Result object from result data.



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

def default_for(target)
  case @data[:default]
  when Bolt::PlanFailure
    # Bolt::PlanFailure needs to be declared before Bolt::Error because
    # Bolt::PlanFailure is an instance of Bolt::Error, so it can match both
    # in this case we need to treat Bolt::PlanFailure's in a different way
    #
    # raise Bolt::PlanFailure errors so that the PAL can catch them and wrap
    # them into Bolt::PlanResult's for us.
    raise @data[:default]
  when Bolt::Error
    Bolt::Result.from_exception(target, @data[:default])
  when Hash
    result_for(target, **Bolt::Util.walk_keys(@data[:default], &:to_sym))
  else
    raise 'Default result must be a Hash'
  end
end

#error_with(data, clazz = Bolt::Error) ⇒ Object

Set a default error result for all targets.



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/bolt_spec/plans/action_stubs.rb', line 180

def error_with(data, clazz = Bolt::Error)
  data = Bolt::Util.walk_keys(data, &:to_s)
  if data['msg'] && data['kind'] && (data.keys - %w[msg kind details issue_code]).empty?
    @data[:default] = clazz.new(data['msg'], data['kind'], data['details'], data['issue_code'])
  else
    $stderr.puts "In the future 'error_with()' might require msg and kind, and " \
                "optionally accept only details and issue_code."
    @data[:default] = data
  end
  @data_set = true
  self
end

#expect_callObject

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



76
77
78
79
80
# File 'lib/bolt_spec/plans/action_stubs.rb', line 76

def expect_call
  @expected_calls = 1
  @expect = true
  self
end

#not_be_calledObject

error if the stub is called at all.



142
143
144
145
# File 'lib/bolt_spec/plans/action_stubs.rb', line 142

def not_be_called
  @expected_calls = 0
  self
end

#return(&block) ⇒ Object



147
148
149
150
151
152
# File 'lib/bolt_spec/plans/action_stubs.rb', line 147

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. May use string or symbol keys, but allowed key names are restricted based on action.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bolt_spec/plans/action_stubs.rb', line 156

def return_for_targets(data)
  data.each_with_object(@data) do |(target, result), hsh|
    raise "Mocked results must be hashes: #{target}: #{result}" unless result.is_a? Hash

    # set the inventory from the BoltSpec::Plans, otherwise if we try to convert
    # this target to a string, it will fail to string conversion because the
    # inventory is nil
    hsh[target] = result_for(Bolt::Target.new(target, @inventory), **Bolt::Util.walk_keys(result, &:to_sym))
  end
  raise "Cannot set return values and return block." if @return_block

  @data_set = true
  self
end

#with_targets(targets) ⇒ Object

Restricts the stub to only match invocations with the correct targets



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bolt_spec/plans/action_stubs.rb', line 122

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