如果要在一行中创建和配置Sinon存根,可以吗?天真的实现:
const a = sinon.stub().withArgs(myArg1).returns(15);
没有将a
设置为存根,而是将其设置为15
。我希望a
等于
const a = sinon.stub();
a.withArgs(myArg1).returns(15);
我看了看文档,但没有发现我-我想要的是可能的吗?
这是returns
的"sinon": "^8.1.1"
方法的签名:
returns(obj: TReturnValue): SinonStub<TArgs, TReturnValue>;
因此returns
方法的返回值是正弦桩。例如:
import sinon from 'sinon';
import { expect } from 'chai';
describe('56876050', () => {
it('should pass', () => {
const a = sinon
.stub()
.withArgs('myArg1')
.returns(15);
const actual = a();
expect(actual).to.eql(15);
sinon.assert.calledOnce(a);
});
});
单元测试结果:
56876050
✓ should pass
1 passing (8ms)