Jasmine spyOn有特定的论点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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'

有什么建议?

答案

你可以使用callFake

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

以上是关于Jasmine spyOn有特定的论点的主要内容,如果未能解决你的问题,请参考以下文章