【问题标题】:How to check the number of arguments that a function has been called with Mocha, Chai and Sinon?如何检查使用 Mocha、Chai 和 Sinon 调用函数的参数数量?
【发布时间】:2017-03-15 11:03:22
【问题描述】:

假设我们有一个服务 Foo 正在导出一个函数

function bar(x,y){
    console.log(x,y);
}

我们想编写一个单元测试来测试这个函数是用 2 个参数调用的。 这个我试过了

var args = sandboxSinon.spy(Foo, 'bar').getCalls()[0].args;

这又回来了

undefined is not an object (evaluating 'sandboxSinon.spy(Foo, 'bar').getCalls()[0].args

有人能弄清楚发生了什么或我如何测试它吗?

【问题讨论】:

    标签: javascript unit-testing sinon karma-mocha sinon-chai


    【解决方案1】:

    这是一个例子:

    const sinon = require('sinon');
    
    const Foo = {
      bar(x,y) {
        console.log(x, y);
      }
    };
    
    let spy = sinon.spy(Foo, 'bar');
    
    Foo.bar('hello', 'world');
    
    console.log( spy.firstCall.args.length ); // => 2
    

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多