Jasmine spyOn有特定的论点

问题描述 投票:16回答:1

假设我有

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'

有什么建议?

jasmine spyon
1个回答
22
投票

你可以使用callFake

spyOn($cookieStore,'get').and.callFake(function(arg) {
    if (arg === 'someValue'){
        return 'someabc';
    } else if(arg === 'anotherValue') {
        return 'anotherabc';
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.