Mole Api - slavik57/moqjs GitHub Wiki

Mole

// Create a mole
var mole = new Mole(new FooClass());

// Destroy the mole
mole.dispose();

// Setup function
mole.setup(foo => foo.doSomething("ping"))...

// Setup getter
mole.setup(foo => foo.getSomething)...

// Setup setter
mole.setup(foo => foo.setSomething = 'some value')...

// Setup private (TypeScript) function
mole.setupPrivate('_privateDoSomethind', "ping")...

// Setup private (TypeScript) getter
mole.setupPrivate('_privateGetSomethind')...

// Setup private (TypeScript) setter
mole.setupPrivate('_privateSetSomethind', "ping")...

// Verify function without arguments
var result = mole.verify(foo => foo.doSomething());

// Verify function with arguments
var result = mole.verify(foo => foo.doSomething(1, 'some value', It.isAny(String)));

// Verify function with arguments with Times
var result = mole.verify(foo => foo.doSomething(1), Times.moreThan(3));

// Verify private (TypeScript) function
var result = mole.verifyPrivate('_privateDoSomething');

// Verify private (TypeScript) function with arguments
var result = mole.verifyPrivate('_privateDoSomething', [1, 'some value', It.isAny(String)]);

// Verify private (TypeScript) setter with Times
var result = mole.verifyPrivate('_privateSetSomething', ['some value'], Times.between(1,3));

// Verify private (TypeScript) setter with Times
var result = mole.verifyPrivate('_privateGetSomething', [], Times.between(1,3));

// Find the mole of an object
var object = // Get the object somehow
var mole = Mole.findMoleByObject(object);

// Set strict behavior (throw if does not have setup)
mole.isStrict = true; // false by default

// Call the original function if does not have setup
mole.callBase = true; // true by default.
mole.callBase = false; // no setup => does not throw error. Simply does nothing

// Set recursive mole (mole will be created for the return value)
mole.moleReturnValue = true; // false by default
var result = foo.doSomething();
var resultMole = Mole.findMoleByObject(result);
resultMole.setup(....)