Ruff 0.8.0 Documentation


Ruff Mock#

The Ruff mock provides you a stub/mock framework in order to create mock/stub and verification. You can include this module with require(ruff-mock);

How to use#

Include the Ruff mock:

var ruffMock = require('ruff-mock');
var mock = ruffMock.mock;
var verify = ruffMock.verify;
...

Let's stub#

// Create mock of your type
var mocked = mock(Foo);

// Stubbing
when(mocked).get('one').thenReturn('two');

// The console should print 'two'
console.log(mock.get('one'));

Any mock#

Any mock can mock an instance of a non-existing type.

// Create any mock
var mocked = anyMock();

// Stubbing
when(mocked).get('one').thenReturn('two');

// The console should print 'two'
console.log(mock.get('one'));

Argument matching#

// Create mock of your type
var mocked = mock(Foo);

// Stubbing
when(mocked).get(any()).thenReturn('two');

// The console should print 'two'
console.log(mock.get('one'));

If you want to directly pass parameters, they have to excatly match the argument matching.

How about verifying#

// Create your mock based on your type
var mocked = mock(Foo);

// Using mock object
mocked.push("one");

// Verification
verify(mocked, once()).push("one");

Ruff mock provides some default verification mode:

  • once make sure the target function has been invoked only once.
  • twice make sure the target function has been invoked only twice.
  • times, you can specify how many times the target function has been invoked.
  • atLeast, how many times the target function has been invoked at least.
  • atMost, how many times the target function has been invoked at most.

The default mode is once.

verify(mocked, once()).push("one");