Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

daredevildave/jack

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Jack is a toolkit for mocking JavaScript objects and functions when writing tests. It is inspired by similar projects for Java and C#, like JMock, Mockito and Rhino Mocks

Mocking can be achieved quite easily in JavaScript without help from a library, but anything above verifying that a function gets called can grow hard to read. If you have experienced this (or want to avoid it in the first place), Jack is for you.

Contributions

Contributions are very welcome. Just fork git://github.com/keronsen/jack.git and get started.

The head developer for Jack is Karl-Erik Rønsen

Contributions have been received, accepted and appreciated from:

Getting started

Download the latest version of Jack from github: http://github.com/keronsen/jack
General examples are found below.
There are examples of use with different test frameworks in /docs/examples.

If you need help, there is a google group for discussion: http://groups.google.com/group/jack-discuss

Test framework integration

Jack is currently compatible with these JavaScript test frameworks:

Integration is planned for these frameworks:

Examples

For these examples, the application under test has a function storeSomething() that we expect will call the jQuery.post() function.

Expect one call to function

jack(function(){
   jack.expect("jQuery.post").exactly("1 time");
   storeSomething();
});

Available call quantifiers: never(), once(), exactly(), atLeast(), atMost()

Expect argument values

jack(function(){
   jack.expect("jQuery.post")
      .exactly("1 time")
      .withArguments("http://example.com/service");
   storeSomething();
});

Set other argument constraints

jack(function(){
   jack.expect("jQuery.post")
      .exactly("1 time")
      .whereArgument(0).isOneOf("/serviceOne","/serviceTwo");
   storeSomething();
});

Available constraint methods: is(), isNot(), isOneOf(), isType(), matches(), hasProperty(), hasProperties()

Specifying a mock implementation of a function

jack(function(){
   jack.expect("jQuery.post")
      .exactly("1 time")
      .mock(function() {
          // your mock implementation
      });
   storeSomething();
});

Specifying a return value

jack(function(){
   jack.expect("jQuery.post")
      .exactly("1 time")
      .returnValue("The value to return.");
   storeSomething();
});

Creating a mock object for testing interaction

If you want to test how your code under test interacts with other objects, you can create a mock object with jack.create():

jack(function(){
   var myStack = jack.create("myStack", ['push','pop']);
   jack.expect("myStack.push")
      .exactly("1 time")
      .whereArgument(0).is("something");
   useTheStack(myStack);
});

Releases

No releases published

Packages

No packages published