使用sinon

时间:2019-02-04 14:07:09

标签: javascript ecmascript-6 tdd sinon chai

我有一个要监视的类来检查调用该方法的参数。

class Animal {
  constructor() {
     this.animals = [];
  }

  add(animal) {
    this.animals.push(animal);
  }
}

我的测试文件如下

const chai = require('chai');
const sinon  = require('sinon');
const Lazy = require('../lazy');

it('should be able to add an animal', function () {
    const animal = new Animal();
    const add = sinon.spy(animal, 'add');
    animal.add('cat')
    expect(animal).to.have.been.called.with('cat');
});

间谍无法正常工作。我想知道如何使用sinon检查被调用的内容。

2 个答案:

答案 0 :(得分:0)

sk_image.transpose(1, 0, 2).reshape(130,-1)是对象,间谍实际上是animal,因此应该是:

add

答案 1 :(得分:0)

Patrick对代码而言是正确的,您需要验证spy而不是对象,但您的代码仍必须经过修改才能运行。我做了一个functioning example on RunKit。似乎您也需要使用calledWith,但是您可能已经使用了其他设置(缺少该设置)。签出代码:-)

因此将期望值更改为

expect(add).to.have.been.calledWith('cat');