Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine conditional callThrough and callFake

I have a method which returns function references.

function methodetobeMoked(param){
  case1:return func1;
  case 2: return func2;
 .
 .
 case n: return funcN;
}

I need to spy this method and return a fake function reference for a particular input param p

Is there any conditional callThrough in jasmine tests My scenario is

SpyOn(some object,'someMethode').and.{if param=p callFake(fakeMethode) else callThrough()}

I tried callFake Is there any way to pass control to original method from fake method?

like image 905
Able Johnson Avatar asked Jan 05 '23 20:01

Able Johnson


1 Answers

A Jasmine spy retains the original function in a property named originalValue, so you can do something like:

var mySpy = {};
mySpy = t.spyOn(obj, 'methodToBeMocked').and.callFake(function (param) {
    if (param === 'fake case') {
        // return fake result
    } else {
        // do this if using Jasmine
        return (mySpy.and.callThrough())(param);
        // do this if using Ext + Siesta and duped by common syntax :)
        // return mySpy.originalValue(param);
    }
});
like image 103
ZagNut Avatar answered Jan 19 '23 05:01

ZagNut