Client.prototype.a = function(x, y, z) {
var results = [];
var result1 = this.foo(x, y, z) ;
results.push(result1);
var result2 = this.bar(x, y, z) ;
results.push(result2);
return results;
}
我需要进行单元测试:
foo
和bar
被x, y and z
调用。result1
和result2
。我正在使用sinon,但是我是sinon测试框架的新手。
答案 0 :(得分:1)
我怀疑您的Client
库看起来像这样:
const Client = function() {};
Client.prototype.foo = () => {};
Client.prototype.bar = () => {};
那么您可以轻松地使用sinon stubs和spies进行测试,而我使用的是chai's expect,因为它可以很好地比较数组/对象:
const sinon = require('sinon');
const {expect} = require('chai');
const client = new Client();
// define simple function that just returns args
const returnArgs = (...args) => args;
// stub foo & bar to return args
sinon.stub(client, 'foo').callsFake(returnArgs);
sinon.stub(client, 'bar').callsFake(returnArgs);
it('should call foo & bar', () => {
const args = [1,2,3];
const actual = client.a(...args);
expect(actual).eqls([args, args]);
expect(client.foo.calledOnce).to.be.true;
expect(client.foo.getCall(0).args).eqls(args);
expect(client.bar.calledOnce).to.be.true;
expect(client.bar.getCall(0).args).eqls(args);
})