Search code examples
javascriptunit-testingmockingyuimockito

Mocking YUI function


I have signup.js, which contains one function name demo(). This function call another YUI library function with arguments:

Y.io('mywrongserver',callbackfunction);

I have to test this function but I want to make call to right server

Y.io('myrightserver',callbackfunction);

How is it possible? I do not want to change any code in signup.js as it is done by developer.

Can we make use of Mocking?

Code would be like this:

signup.js

demo : function(){
    alert('i am inside demo');
    // Some other stuff
    callback = {
        on :{
            success:function(x,o){
                // Some stuff here
            }
        }
    }
    // Now call to wrong server
    Y.io("mywrongserver", callback); 

test.js

// Using JSMOCKITO APIs for YUI
testDemo = function(){
    // Need to test demo function in signup.js But such that Y.io call to right server 
}

Thanks


Solution

  • The answer is indeed "yes". I haven't used jsmockto, but I can answer it it in general terms. Basically, you overwrite Y.io with your own function that tests whether Y.io was passed the correct arguments.