admin管理员组

文章数量:1316616

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?

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?

Share Improve this question asked Jun 21, 2016 at 12:26 Able JohnsonAble Johnson 5717 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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 mon syntax :)
        // return mySpy.originalValue(param);
    }
});

本文标签: javascriptJasmine conditional callThrough and callFakeStack Overflow