class DoubleBuilder

This class serves only to create a context to enable a new domain-specific-language (DSL)

for defining a backend in a simple way. The DoubleBuilder is constructed with the current
test context which it later defines the #backend method that returns the test double that
is built with this DSL.

Public Class Methods

new(&block) click to toggle source
# File lib/templates/resource_pack/spec/spec_helper.rb, line 99
def initialize(&block)
  @content_block = block
end

Public Instance Methods

backend_doubles() click to toggle source

Store all the doubling specified in the initial part of evaluate

# File lib/templates/resource_pack/spec/spec_helper.rb, line 124
def backend_doubles
  @backend_doubles ||= []
end
evaluate(test_context, backend) click to toggle source
# File lib/templates/resource_pack/spec/spec_helper.rb, line 103
def evaluate(test_context, backend)
  # Evaluate the block provided to queue up a bunch of backend double definitions.
  instance_exec(&@content_block)

  backend_doubles = self.backend_doubles
  test_context.instance_exec do
    # With all the backend double definitions defined,
    # create a backend to append all these doubles
    backend_doubles.each do |backend_double|
      if backend_double.has_inputs?
        allow(backend).to receive(backend_double.name).with(*backend_double.inputs).and_return(backend_double.outputs)
      else
        allow(backend).to receive(backend_double.name).with(no_args).and_return(backend_double.outputs)
      end
    end
  end

  backend
end
method_missing(backend_method_name, *args, &_block) click to toggle source
# File lib/templates/resource_pack/spec/spec_helper.rb, line 128
def method_missing(backend_method_name, *args, &_block)
  backend_double = BackendDouble.new(backend_method_name)
  backend_double.inputs = args unless args.empty?
  backend_doubles.push backend_double
  # NOTE: The block is ignored.
  self
end
returns(method_signature_as_hash) click to toggle source

When defining a new aspect of the environment (e.g. command, file) you will often want a result from that detail. Because of the fluent interface this double builder provides this is a way to grab the last build double and append a mock of a return object.

@TODO this shouldn't be used without a double being created, an

error will be generated with that last_double coming back as a nil.
There may be some interesting behavior that could be undertaken
here when no aspect is provided. It may also be better to throw a
useful exception that describes use.
# File lib/templates/resource_pack/spec/spec_helper.rb, line 150
def returns(method_signature_as_hash)
  return_result = InSpecResouceMash.new(method_signature_as_hash)
  last_double = backend_doubles.last
  results_double_name = "#{last_double.name}_#{last_double.inputs}_RESULTS"
  last_double.outputs = RSpec::Mocks::Double.new(results_double_name, return_result)
  self
end