This guide assumes you are familiar with unit-testing and JSUnit.
For a simple example we are going to test a publish/subscribe message system. A Publisher sends objects to zero or more Subscribers. We want to test the Publisher, which involves testing its interactions with its Subscribers.
The Subscriber interface looks like this:
function Subscriber() { } Subscriber.prototype = { receive: function(message) { } }
We will test that a Publisher sends a message to a single registered Subscriber. To test interactions between the Publisher and the Subscriber we will use a mock Subscriber object.
First we must import the Mock4JS classes, define our test fixture class and define a test case method.
<html> <head> ... <script language="JavaScript" type="text/javascript" src=".../jsunit/app/jsUnitCore.js"></script> <script language="JavaScript" type="text/javascript" src=".../mock4js.js"></script> <script language="JavaScript" type="text/javascript" src=".../Publisher.js"></script> <script language="JavaScript" type="text/javascript"> Mock4JS.addMockSupport(this); function setUp() { Mock4JS.clearMocksToVerify(); } function tearDown() { Mock4JS.verifyAllMocks(); } function testOneSubscriberReceivesAMessage() { ... } ...
We will now write the body of the
testOneSubscriberReceivesAMessage
method.
We first set up the context in which our test will execute. We create a Publisher to test. We create a mock Subscriber that should receive the message. We then register the Subscriber with the Publisher. Finally we create a message object to publish.
var mockSubscriber = mock(Subscriber); var publisher = new Publisher(); publisher.add(mockSubscriber.proxy()); var message = "message";
Next we define expectations on the mock Subscriber that specify the
methods that we expect to be called upon it during the test run. We expect
the receive method to be called with a single argument, the message that will
be sent. The eq
method is defined in the MockObjectTestCase
class and specifies a "constraint" on
the value of the argument passed to the subscriber: we expect the argument to
be the equal to the message, but not necessarily the same object.
(Mock4JS provides several constraint
types that can be used to precisely specify expected argument values). We
don't need to specify what will be returned from the receive method because
it has a void return type.
mockSubscriber.expects(once()).receive(message);
We then execute the code that we want to test.
publisher.publish(message);
After the test has finished, the call to Mock4JS.verifyAllMocks() in tearDown() will verify that the mock Subscriber was called as expected. If the expected calls were not made, the test will fail.
Here is the complete test.
Mock4JS.addMockSupport(this); function setUp() { Mock4JS.clearMocksToVerify(); } function tearDown() { Mock4JS.verifyAllMocks(); } function testOneSubscriberReceivesAMessage() { // setup var mockSubscriber = mock(Subscriber); var publisher = new Publisher(); publisher.add(mockSubscriber.proxy()); var message = "message"; // expectations mockSubscriber.expects(once()).receive(message); // execute publisher.publish(message); }