Thursday, October 16th, 2008

Jack: Mocking JavaScript the good way

Category: Testing

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:

javascript

  1. // Expect one call to function
  2. jack(function(){
  3.    jack.expect("jQuery.post").exactly("1 time");
  4.    storeSomething();
  5. });
  6.  
  7. // Expect argument values
  8. jack(function(){
  9.    jack.expect("jQuery.post")
  10.       .exactly("1 time")
  11.       .withArguments("http://example.com/service");
  12.  
  13.    storeSomething();
  14. });
  15.  
  16. // Set other argument constraints
  17. jack(function(){
  18.    jack.expect("jQuery.post")
  19.       .exactly("1 time")
  20.       .whereArgument(0).isOneOf("/serviceOne","/serviceTwo");
  21.  
  22.    storeSomething();
  23. });
  24.  
  25. // Creating a mock object to run expectations against
  26. jack(function(){
  27.    var mockObject = jack.create("mockObject", ['functionOne','functionTwo']);
  28.  
  29.    jack.expect("mockObject.functionOne")
  30.       .exactly("1 time")
  31.       .whereArgument(0).is("something");
  32.  
  33.    mockObject.functionOne("something");
  34. });

Posted by Dion Almaer at 9:37 am
1 Comment

++++-
4 rating from 39 votes

1 Comment »

Comments feed TrackBack URI

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.

Comment by longgoldenears — October 18, 2008

Leave a comment

You must be logged in to post a comment.