I have a JavaScript class that I need to test:
TestClass = function {
// some code that utilizes initializeRequest
this.initializeRequest = function() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
I am attempting to override the initializeRequest method for testing. Here is what I have tried:
var request = new MockXMLHttpRequest();
var instance = new TestClass();
instance.initializeRequest = function() {
return request;
};
// calling the TestClass methods that use initializeRequest
// testing code with assertions for the request
However, when I call the initializeRequest
method, it still executes the original code instead of the function I passed to instance.initializeRequest
.
Any suggestions on what could be the issue here?