module

Spectator::DSL::Mocks

Methods and macros for mocks and doubles.

Constants

TYPES = [] of ::Tuple(Symbol, Symbol, Symbol)

All defined double and mock types. Each tuple consists of the double name or mocked type, defined context (example group), and double type name relative to its context.

Instance methods

allow(stubbable : Stubbable | StubbedType)

Targets a stubbable object (such as a mock or double) for operations.

The stubbable must be a Stubbable or StubbedType. This method is expected to be followed up with .to receive().

dbl = dbl(:foobar)
allow(dbl).to receive(:foo).and_return(42)
Source
allow(stubbable)

Helper method producing a compilation error when attempting to stub a non-stubbable object.

Triggered in cases like this:

allow(42).to receive(:to_s).and_return("123")
Source
any_args

Indicates any arguments can be used (no constraint).

Source
no_args

Returns empty arguments.

Source

Macros

double(name, **value_methods, &block)

Defines or instantiates a double.

When used inside of a method, instantiates a new double. See #new_double.

When used outside of a method, defines a new double. See #def_double.

Source
double

Instantiates a new double with predefined responses.

This constructs a LazyDouble.

dbl = double(foo: 42)
expect(dbl.foo).to eq(42)
Source
inject_mock(type, **value_methods, &block)

Injects mock (stub) functionality into an existing type.

Warning: Using this will modify the type being tested. This may result in different behavior between test and non-test code.

This must be used instead of def_mock if a concrete struct is tested. The mock method is not necessary to create a type with an injected mock. The type can be used as it would normally instead. However, stub information may leak between examples.

The type is the name of the type to inject mock functionality into. Initial stubbed values for methods can be provided with value_methods.

struct MyStruct
  def foo
    42
  end
end

inject_mock(MyStruct, foo: 5)

specify do
  inst = MyStruct.new
  expect(inst.foo).to eq(5)
  allow(inst).to receive(:foo).and_return(123)
  expect(inst.foo).to eq(123)
end
Source
mock(type, **value_methods, &block)

Defines or instantiates a mock.

When used inside of a method, instantiates a new mock. See #new_mock.

When used outside of a method, defines a new mock. See #def_mock.

Source
receive(method, *, _file = __FILE__, _line = __LINE__, &block)

Begins the creation of a stub.

The method is the name of the method being stubbed. It should not define any parameters, it should be just the method name as a literal symbol or string.

Alone, this method returns a NullStub, which allows a stubbable object to return nil from a method. This macro is typically followed up with a method like and_return to change the stub's behavior.

dbl = dbl(:foobar)
allow(dbl).to receive(:foo)
expect(dbl.foo).to be_nil

allow(dbl).to receive(:foo).and_return(42)
expect(dbl.foo).to eq(42)

A block can be provided to be run every time the stub is invoked. The value returned by the block is returned by the stubbed method.

dbl = dbl(:foobar)
allow(dbl).to receive(:foo) { 42 }
expect(dbl.foo).to eq(42)
Source