Function Setup Api - slavik57/moqjs GitHub Wiki

Setup of property getters/setters or functions

var foo = new FooClass();
var mole = new Mole(foo);

// returns the given value
mole.setup(foo => foo.doSomething()).returns(1);
var result = foo.doSomething(); // Will return 1

// returns the given values by order of calls
mole.setup(foo => foo.doSomething()).returnsInOrder(1, 2, 'some value');
var result1 = foo.doSomething(); // Will return 1
var result2 = foo.doSomething(); // Will return 2
var result3 = foo.doSomething(); // Will return 'some value'
var result4 = foo.doSomething(); // Will return undefined

// return the lazy calculated value
var value = 1;
var calculate = function(){
   value++;
   return value;
}
mole.setup(foo => foo.doSomething()).lazyReturns(calculate);

// return the lazy calculated values in order
mole.setup(foo => foo.doSomething()).lazyReturnsInOrder([calculate1, calculate2]);

// All the lazy returns can be passed an argument
var calculate = function(arg){
   return 1 + arg;
}
mole.setup(foo => foo.doSomething(It.isAny(Number))).lazyReturns(calculate);
var result = foo.doSomething(2); // Will return 3

// Set getter return value
mole.setup(foo => getSomething).returns(3);

// Throw error
mole.setup(foo => foo.doSomething()).throws('some error');

// Throw lazy calculated error
var createError = function(argName){
   throw 'The argument ' + argName + ' is null';
}
mole.setup(foo => foo.doSomething(null)).lazyThrows(createError);

// Throw if getter or setter is called
mole.setup(foo => getSomething).throws('get error');
mole.setup(foo => setSomething = 1).throws('cannot set the number 1'); 

// Call a function when property or function is called
var someFunction = function() { ... };
mole.setup(foo => foo.doSomething()).callback(someFunction);

// Call a function when getter or setter is called
var onGetterCalled = function() { ... };
var onSetterCalled = function(val) { ... };
mole.setup(foo => foo.getSomething).callback(onGetterCalled);
mole.setup(foo => foo.setSomething = It.isAny(String)).callback(onSetterCalled);

// Setup different behaviors for different arguments
mole.setup(foo => foo.doSomething(2)).returns(1);
mole.setup(foo => foo.doSomething(It.isAny(String))).throws('some error');
mole.setup(foo => foo.doSomething(It.isAny(Number))).callback(function(num){
   console.log(num);
});

foo.doSomething('sss'); // Will throw error
foo.doSomething(22); // Will write log
foo.dosomething(2);  // Will return 1 and write to log

Setup of private property getters/setters or functions

var foo = new FooClass();
var mole = new Mole(foo);

// returns the given value
mole.setupPrivate('_doSomething').returns(1);

// returns the given values by order of calls
mole.setupPrivate('_doSomething').returnsInOrder(1, 2, 'some value');

// return the lazy calculated value
var value = 1;
var calculate = function(){
   value++;
   return value;
}
mole.setupPrivate('_doSomething').lazyReturns(calculate);

// return the lazy calculated values in order
mole.setupPrivate('_doSomething').lazyReturnsInOrder([calculate1, calculate2]);

// All the lazy returns can be passed an argument
var calculate = function(arg){
   return 1 + arg;
}
mole.setupPrivate('_doSomething', It.isAny(Number)).lazyReturns(calculate);

// Set getter return value
mole.setupPrivate('_getSomething').returns(3);

// Throw error
mole.setupPrivate('_doSomething').throws('some error');

// Throw lazy calculated error
var createError = function(argName){
   throw 'The argument ' + argName + ' is null';
}
mole.setupPrivate('_doSomething', null).lazyThrows(createError);

// Throw if getter or setter is called
mole.setupPrivate('_getSomething').throws('get error');
mole.setupPrivate('_setSomething', 1).throws('cannot set the number 1'); 

// Call a function when property or function is called
var someFunction = function() { ... };
mole.setupPrivate('_doSomething').callback(someFunction);

// Call a function when getter or setter is called
var onGetterCalled = function() { ... };
var onSetterCalled = function(val) { ... };
mole.setupPrivate('_getSomething').callback(onGetterCalled);
mole.setupPrivate('_setSomething', It.isAny(String)).callback(onSetterCalled);

// Setup different behaviors for different arguments
mole.setupPrivate('_doSomething', 2).returns(1);
mole.setupPrivate('_doSomething', It.isAny(String)).throws('some error');
mole.setupPrivate('_doSomething', It.isAny(Number)).callback(function(num){
   console.log(num);
});