Jasmine - Two spies for the same method

醉酒当歌 提交于 2019-12-11 21:22:56

问题


I'm new to Jasmine and I wanted to know if we can create 2 spies for the same method. Here is what I'm trying.

describe('something', function () {
    beforeEach(function () {
        mySpy = jasmine.createSpyObj('mySpy', 'functionInInterest');
        mySpy.functionInInterest.andCallFake(function (cb) {cb(something);});
    }

    //Some Test Cases
    describe('Here is the action!', function () {
        mySpy = jasmine.createSpyObj('mySpy', 'functionInInterest');
        mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);});
        //Some test cases that depends on somethingElse
    });
});

Testcases before Here is the action! depend on mySpy.functionInInterest.andCallFake(function (cb) {cb(something);}); where as test cases inside Here is the action! depend on mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);});

Note: Both have the same name

How can I achieve this? Thanks in advance!


回答1:


instead of

describe('Here is the action!', function () {
        mySpy = jasmine.createSpyObj('mySpy', 'functionInInterest');
        mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);});
        //Some test cases that depends on somethingElse
    });

do this

describe('Here is the action!', function () {
        mySpy_2 = jasmine.createSpyObj('mySpy', 'functionInInterest');
        mySpy_2.functionInInterest.andCallFake(function (cb) {cb(somethingElse);});
        //Some test cases that depends on somethingElse
    });


来源:https://stackoverflow.com/questions/18437035/jasmine-two-spies-for-the-same-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!