I am stuck to test the function that use $j(window).width(). This function return value according to this.Now my problem is this that i want to set window width in my Qunit function.Can any body help ???
In situations when you are expecting a value returned from an object that you don't control or at least need to test, you should mock the return values. Take a look at JSMockito which is a JavaScript based mocking framework. Below is a simple example that mocks an Array object and sets the value to be returned when the test calls get(1)
method.
var mockedObject = mock(Array);
when(mockedObject).get(1).thenReturn("hello world");
// -- start code under test --
alert(mockedObject.get(1));
// the following alerts 'true' as get(99) was not stubbed
alert(typeof (mockedObject.get(99)) === 'undefined');
// -- end code under test --
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.