Mock4JS: stubs() versus expects()

In the previous examples, I have used expects() like this:

mockObject.expects(once()).someMethod().will(returnValue("result"))

This says that the mock object expects exactly one call to someMethod(). If there is not exactly one call, the test will fail. If for a particular test case it is not interesting to make this assertion, then you should use stubs() instead:

mockObject.stubs().someMethod().will(returnValue("result"))

In this case, the mock object does not care how many times someMethod() is called, but each time it does get called it will return "result"

Use of stubs() for uninteresting/unimportant method invocations is recommended because it helps keep the test focussed on the specific test case intent. Additionally, any changes in the real object's behaviour will break less tests.


SourceForge.net Logo