Class: BoltSpec::Plans::ActionStub

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

Direct Known Subclasses

CommandStub, PublishStub, ScriptStub, TaskStub, UploadStub

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expect = false) ⇒ ActionStub

Returns a new instance of ActionStub.



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

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

Instance Attribute Details

#invocationObject (readonly)

Returns the value of attribute invocation.



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

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.



139
140
141
142
143
# File 'lib/bolt_spec/plans/action_stubs.rb', line 139

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

#assert_called(object) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bolt_spec/plans/action_stubs.rb', line 46

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]
    message += " with parameters #{parameters}" if 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.



108
109
110
111
# File 'lib/bolt_spec/plans/action_stubs.rb', line 108

def be_called_times(times)
  @expected_calls = times
  self
end

#check_resultset(result_set, object) ⇒ Object



83
84
85
86
87
88
# File 'lib/bolt_spec/plans/action_stubs.rb', line 83

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.



72
73
74
75
76
77
78
79
80
81
# File 'lib/bolt_spec/plans/action_stubs.rb', line 72

def default_for(target)
  case @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) ⇒ Object

Set a default error result for all targets.



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bolt_spec/plans/action_stubs.rb', line 146

def error_with(data)
  data = Bolt::Util.walk_keys(data, &:to_s)
  if data['msg'] && data['kind'] && (data.keys - %w[msg kind details issue_code]).empty?
    @data[:default] = Bolt::Error.new(data['msg'], data['kind'], data['details'], data['issue_code'])
  else
    STDERR.puts "In the future 'error_with()' may 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.



65
66
67
68
69
# File 'lib/bolt_spec/plans/action_stubs.rb', line 65

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

#not_be_calledObject

error if the stub is called at all.



114
115
116
117
# File 'lib/bolt_spec/plans/action_stubs.rb', line 114

def not_be_called
  @expected_calls = 0
  self
end

#return(&block) ⇒ Object



119
120
121
122
123
# File 'lib/bolt_spec/plans/action_stubs.rb', line 119

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.



127
128
129
130
131
132
133
134
135
# File 'lib/bolt_spec/plans/action_stubs.rb', line 127

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
    hsh[target] = result_for(Bolt::Target.new(target), 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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bolt_spec/plans/action_stubs.rb', line 94

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