module XSpec::Evaluator::Doubles

### Doubles

The doubles module provides test doubles that can be used in-place of real objects.

Public Class Methods

with(*opts) click to toggle source

It can be configured with the following options:

  • `strict` forbids doubling of classes that have not been loaded. This

should generally be enabled when doing a full spec run, and disabled when running specs in isolation.

The `with` method returns a module that can be included in a stack.

# File lib/xspec/evaluators.rb, line 109
def self.with(*opts)
  modules = [self] + opts.map {|x| {
    strict: Strict
  }.fetch(x) }


  Module.new do
    modules.each do |m|
      include m
    end
  end
end

Public Instance Methods

_double(klass, type) click to toggle source

If the doubled class has not been loaded, a null object reference is used that allows expecting of all methods.

# File lib/xspec/evaluators.rb, line 138
def _double(klass, type)
  ref = if self.class.const_defined?(klass)
    type.new(self.class.const_get(klass))
  else
    StringReference.new(klass)
  end

  Double.new(ref)
end
call(unit_of_work) click to toggle source
Calls superclass method
# File lib/xspec/evaluators.rb, line 98
def call(unit_of_work)
  super
end
class_double(klass) click to toggle source

Simarly, a class double validates that class responds to all expected methods, if that class has been loaded.

# File lib/xspec/evaluators.rb, line 132
def class_double(klass)
  _double(klass, ClassReference)
end
instance_double(klass) click to toggle source

An instance double stands in for an instance of the given class reference, given as a string. The class does not need to be loaded, but if it is then only public instance methods defined on the class are able to be expected.

# File lib/xspec/evaluators.rb, line 126
def instance_double(klass)
  _double(klass, InstanceReference)
end
stub(obj) click to toggle source

All messages sent to a double will return `nil`. Use `stub` to specify a return value instead: `stub(double).some_method(1, 2) { “return value” }`. This must be called before the message is sent to the double.

# File lib/xspec/evaluators.rb, line 160
def stub(obj)
  Proxy.new(obj, :_stub)
end
verify(obj) click to toggle source

Use `verify` to assert that a method was called on a double with particular arguments. Doubles record all received messages, so `verify` should be called at the end of your test.

# File lib/xspec/evaluators.rb, line 151
def verify(obj)
  Proxy.new(obj, :_verify)
end