简体   繁体   English

如何使用Sinon存根对象?

[英]How to stub an object using Sinon?

I'm wondering if/how i can use Sinon to stub the object mappedValues nested in the function below: 我想知道是否/如何使用Sinon mappedValues嵌套在以下函数中的对象mappedValues

function getValueFromObj(key) {
    var mappedValues = {
        "a": "aaa",
        "b": "bbb",
        "c": "ccc",
        "d": "ddd",
    };

    return mappedValues[key];
}

My goal for the test is not to check each value but rather just make sure that when passed a key a correctly corresponding value is returned and the value is not augmented in any way. 我测试的目标不是检查每个值,而是只是确保在通过key时返回正确对应的值,并且不会以任何方式增加该值。

I think the best way to test this is to have the test stub mappedValues and then check the return value of getValueFromObj but i'm not sure how to actually stub the object. 认为最好的测试方法是mappedValues测试存根mappedValuesmappedValues ,然后检查getValueFromObj的返回值,但我不确定如何实际存根该对象。

I don't think is a good idea to use sinon for this purpose. 我认为将sinon用于此目的不是一个好主意。 I think you can simply write a test like this: 我认为您可以简单地编写如下测试:

expect(getValueFromObj("a")).to.eql("aaa");

when this expectation (i use chai expect to write that) is satisfied you are sure that all work correctly. 当这个期望(我用chai期望写的)得到满足时,您可以确保所有工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM