class XSpec::Evaluator::Doubles::Double
Since the double object inherits from `BasicObject`, virtually every method call will be routed through `method_missing`. From there, the message can be recorded and an appropriate return value selected from the stubs.
Public Class Methods
new(klass)
click to toggle source
# File lib/xspec/evaluators.rb, line 182 def initialize(klass) @klass = klass @expected = [] @received = [] end
Public Instance Methods
_stub(args, &ret)
click to toggle source
The two methods needed on this object to set it up and verify it are prefixed by `_` to try to ensure they don't clash with any method expectations. While not fail-safe, users should only be using expectations for a public API, and `_` is traditionally only used for private methods (if at all).
# File lib/xspec/evaluators.rb, line 207 def _stub(args, &ret) @klass.validate_call! args @expected << [args, ret] end
_verify(args)
click to toggle source
# File lib/xspec/evaluators.rb, line 213 def _verify(args) @klass.validate_call! args i = @received.index(args) if i @received.delete_at(i) else name, rest = *args ::Kernel.raise EvaluateFailed, "Did not receive: %s(%s)\nDid receive:%s\n" % [ name, [*rest].map(&:inspect).join(", "), @received.map {|name, *args| " %s(%s)" % [name, args.map(&:inspect).join(", ")] }.join("\n") ] end end
method_missing(*actual_args)
click to toggle source
# File lib/xspec/evaluators.rb, line 188 def method_missing(*actual_args) stub = @expected.find {|expected_args, ret| expected_args == actual_args } ret = if stub stub[1].call end @received << actual_args ret end