Quickstart - slavik57/moqjs GitHub Wiki
MoqJQ is intended to be simple to use, strong typed and minimalistic. Based on the .Net Moq library.
You can find the api's:
How to setup properties or functions
How to verify properties/function calls
var mole = new Mole(new FooClass());
mole.setup(foo => foo.doSomething("ping")).returns(true);
// access invocation arguments when returning a value
mole.setup(x => x.doSomething(It.IsAny(String)))
.returns((s: string) => s + '!!!');
// throwing when invoked
mole.setup(foo => foo.doSomething("reset"))
.throws('invalid operation');
mole.setup(foo => foo.doSomething(""))
.throws({ error: 'the argument cant be empty string'});
// lazy evaluating return value
mole.setup(foo => foo.getCount()).lazyReturns(() => count);
// returning different values on each invocation
var mole = new Mole(new FooClass());
var calls = 0;
mole.setup(foo => foo.getCountThing())
.lazyReturns(() => calls)
.callback(() => calls++);
// returns 0 on first invocation, 1 on the next, and so on
// any value
mole.setup(foo => foo.doSomething(It.isAny(String))).returns(true);
// matching lazy evaluated
mole.setup(foo => foo.add(It.is(i => i % 2 == 0))).returns(true);
// matching ranges
mole.setup(foo => foo.add(It.isInRange<int>(0, 10))).returns(true);
// matching regex
mole.setup(x => x.doSomething(It.isRegExp(new RegExp("[a-d]+")))).returns("foo");
mole.setup(foo => foo.name).returns("bar");
// expects an invocation to set the value to "foo"
mole.setup(foo => foo.name = "foo");
// or verify the setter directly
mole.verify(foo => foo.name = "foo");
var mole = new Mole(new FooClass());
mole.setup(foo => foo.execute("ping"))
.returns(true)
.callback(() => calls++);
// access invocation arguments
mole.setup(foo => foo.execute(It.isAny(String)))
.returns(true)
.callback((s: string) => calls.push(s));
// access arguments for methods with multiple parameters
mole.setup(foo => foo.execute(It.isAny(Number), It.isAny(String)))
.returns(true)
.callback((i: number, s: string) => calls.push(s));
// callbacks can be specified before and after invocation
mole.setup(foo => foo.execute("ping"))
.callback(() => console.log("Before returns"))
.Returns(true)
.Callback(() => console.log("After returns"));
var result = mole.verify(foo => foo.execute("ping"));
// Method should never be called
mole.verify(foo => foo.execute("ping"), Times.exact(0));
// Called at least once
mole.verify(foo => foo.execute("ping"), Times.atLeast(1));
mole.verify(foo => foo.name);
// Verify setter invocation, regardless of value.
mole.verify(foo => foo.name = It.is(() => true));
// Verify setter called with specific value
mole.verify(foo => foo.name ="foo");
// Verify setter with an argument matcher
mole.verify(foo => foo.value = It.isInRange(1, 5));
-
Make mole behave by raising exceptions for anything that doesn't have a corresponding expectation: a "Strict" mole; mole.isStrict = true;
-
Invoke base class implementation if no expectation overrides the member: default is true.
mole.callBase = true;
-
Setting expectations for private members (TypeScript) (you can't get intellisense for these, so you access them using the member name as a string):
mole.setupPrivate("Execute").returns(5); // if you need argument matching, you can pass them // after the function/property name mole.setup("Execute",5, It.isAny(String), 'some third value') .returns(true);
// get mole from a moled instance
var foo = // get moled object somehow
var fooMole = Mole.findMoleByObject(foo);
fooMole.setup(f => f.submit()).returns(true);
// custom matchers
mole.setup(foo => foo.Submit(It.is((s: string) => !s && s.length > 100)))
.throws('too long string');