QUnit Mock provides some mock assertions and stubs for QUnit.
Just download the qunit.mock.js and include it after QUnit
.
See an example of expecting calls:
MyClass = function() {}
MyClass.prototype = { methodOne: function() { this.methodTwo(); },
methodTwo: function() {},
methodThree: function() { this.methodTwo(); this.methodTwo(); } };
test("calls second method on first", function() { obj = new MyClass(); expectCall(obj, "methodTwo"); // expect the method call obj.methodOne(); });
test("calls second method two times on third", function() { obj = new MyClass(); expectCall(obj, "methodTwo", 2); // this time saying number of expect calls obj.methodThree(); });
At end of test it will automatic check if the method is called and will test it.
See an example of stubbing methods:
MyClass = function() {}
MyClass.prototype = { someMethod: function() { return "hello"; } }
obj = new MyClass();
test("stubbing method", function() { stub(obj, "someMethod", function() { return "world" }); equal(obj.someMethod(), "world"); // ok, will pass });
test("method after stub method", function() { equal(obj.someMethod(), "hello"); // ok too });
Stubbing is easy, also, note in second test the method returned to original state, after the test finish all stubs will be removed and object will keep original ones.