Class: DoubleBuilder
- Inherits:
-
Object
- Object
- DoubleBuilder
- Defined in:
- lib/templates/resource_pack/spec/spec_helper.rb
Overview
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.
Defined Under Namespace
Classes: BackendDouble, InSpecResouceMash
Instance Method Summary collapse
-
#backend_doubles ⇒ Object
Store all the doubling specified in the initial part of #evaluate.
- #evaluate(test_context, backend) ⇒ Object
-
#initialize(&block) ⇒ DoubleBuilder
constructor
A new instance of DoubleBuilder.
- #method_missing(backend_method_name, *args, &_block) ⇒ Object
-
#returns(method_signature_as_hash) ⇒ Object
When defining a new aspect of the environment (e.g. command, file) you will often want a result from that detail.
Constructor Details
#initialize(&block) ⇒ DoubleBuilder
Returns a new instance of DoubleBuilder.
99 100 101 |
# File 'lib/templates/resource_pack/spec/spec_helper.rb', line 99 def initialize(&block) @content_block = block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(backend_method_name, *args, &_block) ⇒ Object
128 129 130 131 132 133 134 |
# 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 |
Instance Method Details
#backend_doubles ⇒ Object
Store all the doubling specified in the initial part of #evaluate
124 125 126 |
# File 'lib/templates/resource_pack/spec/spec_helper.rb', line 124 def backend_doubles @backend_doubles ||= [] end |
#evaluate(test_context, backend) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# 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 |
#returns(method_signature_as_hash) ⇒ Object
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.
150 151 152 153 154 155 156 |
# 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 |