茉莉花 - 同样方法的两个间谍 [英] Jasmine - Two spies for the same method

查看:112
本文介绍了茉莉花 - 同样方法的两个间谍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Jasmine 的新手,我想知道我们是否可以为同一方法创建2个间谍。这是我正在尝试的。

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
    });
});

之前的测试用例这是行动!依赖于 mySpy.functionInInterest.andCallFake(function(cb){cb(something);}); 作为中的测试用例,这是动作!依赖于 mySpy.functionInInterest.andCallFake(function(cb){cb(somethingElse);});

注意:两者名称相同

如何实现这一目标?提前致谢!

How can I achieve this? Thanks in advance!

推荐答案

而不是

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
    });

这样做

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
    });

这篇关于茉莉花 - 同样方法的两个间谍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆