Description and Quickstart
ciscoheat edited this page Oct 13, 2010
·
9 revisions
umock is a library for creating mock objects of Interfaces and (when possible) Classes.
Consider this little interface:
interface IPoint {
var x : Int;
var y : Int;
function length() : Int;
}
With umock, we can mock this interface like this:
import umock.Mock;
...
var mock = new Mock<IPoint>(IPoint);
mock.setupField("x").returns(10);
mock.setupField("y").throws("One dimension only, thank you.");
mock.setupMethod("length").returns(1000);
Setting parameter constraints is also possible:
mock.setupMethod("someMethod").withParams(It.IsAny(Int), "123").returns(1000);
mock.setupMethod("otherMethod").withParams(It.IsRegex(~/[0-9]{5}/), It.IsNull()).returns(true);
So are callbacks and lazy return evaluation:
mock.setupMethod("someMethod").returns(1000).callBack(function() { i++; });
mock.setupMethod("someMethod").returnsLazy(function() { return 100+i; });
mock.setupMethod("someMethod").throwsLazy(function() { return "This string will be thrown"; });
New in version 0.3 is the ability to send the function call parameters to the callback:
mock.setupMethod("someMethod").callBackArgs(function(args : Array<Dynamic>) { trace(args); });
And finally, verifying how many times a method was called:
mock.verify("length", Times.atLeastOnce());
mock.verify("length", Times.never());
mock.verify("length", Times.atMost(3));
Right now the following platforms are supported:
- Neko
- Php
- Javascript
This library is heavily influenced by the great Moq library for .NET.
If you're using the nightly build of haXe which has Macro functionality, check out Strongly typed setup with Macros.