It may be a hoax, but I believe that you should be a mocking function outside of your functional unit, that your testing and it is especially important that your unit tests cannot get into the Live DOM, server or browser.
Check out SinonJS , this is great for that.
Here is an example of testing the initialization method of the router without affecting the state of the browser history.
test('...', function () { // Arrange var buildAppPaths = sinon.stub(ReversibleRouter.prototype, '_buildAppPaths'); var startHistory = sinon.stub(Backbone.history, 'start'); var options = {}; // Act new ReversibleRouter(options); // Assert ok(buildAppPaths.calledWith(options)); ok(startHistory.calledOnce); // or ok(Backbone.history.start.calledOnce); });
With this approach, you successfully claim that all calls were made to external blocks of functionality, including parameters. At this point, you can believe that the external call will do its job (verified by additional unit tests).
To make calls outside this single unit, integration tests will be run, not unit tests.
source share