1

I have such JS class that have to be tested:

SomeClass = function {  
    // some stuff that uses initRequest    
    this.initRequest = function() {  
        if (window.XMLHttpRequest) {  
            return new XMLHttpRequest();  
        } else if (window.ActiveXObject) {  
            return new ActiveXObject("Microsoft.XMLHTTP");  
        }   
     }  
}

I want to override method initRequest for testing purposes. I tried to do something like that

var request = new MockXmlHttpRequest();  
var instance = new SomeClass();
instance.initRequest = function() {
    return request;
};
// some calls of the SomeClass methods that use initRequest
// some test code with assertions for the request

Still calling of the initRequest method calls actually the original code, but not the function that I tried to pass to instance.initRequest.

Any ideas what's wrong?

DixonD
  • 6,557
  • 5
  • 31
  • 52

5 Answers5

0
SomeClass.prototype.initRequest = function() 
  {  
  return request;
  }  
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
0

Check out YUI's extend functionality. It makes all this really clean, PLUS, you can still access the method of the super class if you really want to. Their augment object might also be of interest to you.

Here is the link http://developer.yahoo.com/yui/examples/yahoo/yahoo_extend.html

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
0

In your first example, initRequest returns a new object after each call; in the second example, all calls to initRequest return a reference to the one and only (mock) object.

The request object is not changed in the code example you provide. instance.initRequest simple returns a reference to request. Why do expect it to have changed?

Upperstage
  • 3,747
  • 8
  • 44
  • 67
  • Because of // some calls of the SomeClass methods that uses initRequest – DixonD Nov 20 '09 at 02:13
  • Have you used FireBug to examine request before and after the call? I find FireBug indispensable. Maybe you could post additional code? – Upperstage Nov 20 '09 at 18:16
0

I'm having a little trouble understanding your question because of your English, but I'll try to answer anyway (and if I answer the wrong question just let me know).

I think what you want to do is:

SomeClass = function {  
    // some stuff that uses initRequest    
    this.initRequest = function() {  
        return request;
     }  
}

In other words, you want to overwrite the original SomeClass object with a new function. Unless you do that new SomeClass objects won't use your test method, they'll just use the original method.

However, if you only want to override that method for a specific object, not for the whole class, what you have there should work. If that's the case, could you please clarify what exactly isn't working about it?

machineghost
  • 33,529
  • 30
  • 159
  • 234
-1

First of all, this code sample is not the best way of OOP in JavaScript - use prototype instead. Another remark, your sample throws error in my browser (invalid declarartion of SomeClass function, maybe it's only typing mistake). And at last, instance.initRequest actually returns (I've run it at Google Chrome) object you have expected - check please my sample.

ajukraine
  • 561
  • 9
  • 27