Using Mocks

This example is based on Examples/Tests/Mocks and Stubs/MockTests.js

Many browsers provide a DOM implementation, and typically include objects such as window, document, and XMLHttpRequest. While some console host environments might provide analogues to these, situations arise in which objects will either not exist, or need to be temporarily modified in order to maintain boundary control.


Function.RegisterNamespace("Test"); [Fixture] Test.MockTests=function(){ [Fact] function MocksWindowReferenceOnHost(){ var expected="expected"; var mockWindow=Mocks.GetMock(Object.Global(), "window", expected); var actual=null; mockWindow(function(){ actual=window; }); Assert.Equal(expected, actual); } }

In this example, we override the (usually undefined) reference to window on the global this object, provided by the Object.Global() method, with our well-known expectation. For the purpose of this test, we don't even care about the shape of the object, just that the reference is set to our expectation.