茉莉花spyOn与特定的论点 [英] Jasmine spyOn with specific arguments

查看:146
本文介绍了茉莉花spyOn与特定的论点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

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

对于我的用例而言,这太笼统了.任何时候打电话给我们

This is too general for my use case. Anytime we call

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

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

I want to setup a spyOn so I get different returns based on the argument:

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

这篇关于茉莉花spyOn与特定的论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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