Thursday, October 16th, 2008
Jack: Mocking JavaScript the good way
<p>Jack is a new JavaScript mocking framework coming out of Bekk in Norway. A lot of the RSpec talent is there too, so they know their testing!Jack features:
- Set expectations for number of calls and argument constraints
- Specify mock implementations for specific expectation scenarios
- Create mock objects with a list of stub functions
- Reports expectation failures to test frameworks. Currently integrated with JSSpec and Scriptaculous TestRunner
Examples show it off:
-
-
// Expect one call to function
-
jack(function(){
-
jack.expect("jQuery.post").exactly("1 time");
-
storeSomething();
-
});
-
-
// Expect argument values
-
jack(function(){
-
jack.expect("jQuery.post")
-
.exactly("1 time")
-
-
storeSomething();
-
});
-
-
// Set other argument constraints
-
jack(function(){
-
jack.expect("jQuery.post")
-
.exactly("1 time")
-
.whereArgument(0).isOneOf("/serviceOne","/serviceTwo");
-
-
storeSomething();
-
});
-
-
// Creating a mock object to run expectations against
-
jack(function(){
-
var mockObject = jack.create("mockObject", ['functionOne','functionTwo']);
-
-
jack.expect("mockObject.functionOne")
-
.exactly("1 time")
-
.whereArgument(0).is("something");
-
-
mockObject.functionOne("something");
-
});
-
Related Content:











jqMock was my attempt at writing a javascript mock library. It has comprehensive expectation support, with complex argument matching criteria, ordered/unordered expectations, multiplicity control, intercepting return values and throwing exceptions.