【发布时间】:2019-07-22 19:15:48
【问题描述】:
我有以下代码。 Sinon 未能模拟 doSomething() 并打印实际字符串而不是 'hello'
//file.js
import { doSomething } from 'my-npm-package';
module.exports = () => doSomething();
这是测试文件:
//file.spec.js
import sinon from 'sinon';
import { expect } from 'chai';
import * as apis from 'my-npm-package';
import someFunction from '../file';
describe('TEST', () => {
let stub;
beforeEach(() => {
stub = sinon.stub(apis, 'doSomething').returns('hello');
});
afterEach(() => {
stub.restore();
});
it('test', async () => {
someFunction();
expect(stub.calledOnce).to.equal(true);
});
});
【问题讨论】:
-
这对我来说按预期工作。这可能是编译的问题,您是在使用
webpack还是在使用babel做任何不寻常的事情? -
当我使用
sinon.assert.calledOnce(apis.doSomething);我得到` AssertError: function doSomething(_x5, _x6) { return _ref2.apply(this, arguments); } 没有被存根 ` -
使用
--require babel-polyfill
标签: node.js sinon sinon-chai