问题 Jasmine spyOn有特定的论点


假设我有

spyOn($cookieStore,'get').and.returnValue('abc');

这对我的用例来说太笼统了。我们随时打电话

$cookieStore.get('someValue') -->  returns 'abc'
$cookieStore.get('anotherValue') -->  returns 'abc'

我想设置一个spyOn,所以我根据参数得到不同的回报:

$cookieStore.get('someValue') -->  returns 'someabc'
$cookieStore.get('anotherValue') -->  returns 'anotherabc'

有什么建议么?


2973
2018-05-04 18:28


起源

你看过这个问题了吗: stackoverflow.com/questions/16198353/... - Andrew
可能重复 有没有办法根据参数修改Jasmine间谍? - Fabricio Lemos


答案:


您可以使用 callFake

spyOn($cookieStore,'get').and.callFake(function(arg) {
    if (arg === 'someValue'){
        return 'someabc';
    } else if(arg === 'anotherValue') {
        return 'anotherabc';
    }
}

14
2017-07-08 10:41