readFileSync或safeLoad上的回调函数存根

问题描述 投票:0回答:1

我正在尝试存根以下内容:

let file = yaml.safeLoad(fsExtra.readFileSync(filepath, 'utf8'), err => {
  logger.warn(err);
});

以某种方式获得err并运行logger.warn

但是问题是,当我使用以下任何代码对safeLoadreadFileSync进行存根处理时,它永远不会到达logger.warn(err)

[sinon.stub().throws()sinon.stub().reject()

sinon.stub().callsFake(() => {
    throw new Error();
})

任何想法?

node.js unit-testing sinon stubbing sinon-chai
1个回答
0
投票

这里是解决方案,为简单起见,我创建了一个fsExtra对象来模拟真实节点模块。

index.ts

export const logger = {
  warn(message) {
    console.warn(message);
  }
};

export const yaml = {
  safeLoad(file, callback) {}
};

export const fsExtra = {
  readFileSync(filepath, options) {}
};

export function main() {
  const filepath = './.tmp/sinon.js';
  let file = yaml.safeLoad(fsExtra.readFileSync(filepath, 'utf8'), err => {
    logger.warn(err);
  });
}

index.spec.ts

import { main, fsExtra, yaml, logger } from '.';
import sinon from 'sinon';
import { expect } from 'chai';

describe('main', () => {
  it('should safeLoad error', done => {
    const mError = new Error('fake error');
    const warnSpy = sinon.spy(logger, 'warn');
    const readFileSyncStub = sinon.stub<any, string>(fsExtra, 'readFileSync').returns('file content');
    const safeLoadStub = sinon.stub(yaml, 'safeLoad').callsFake((content, callback) => {
      callback(mError);
      done();
    });
    main();
    expect(readFileSyncStub.calledWith('./.tmp/sinon.js', 'utf8')).to.be.true;
    expect(safeLoadStub.calledWith('file content', sinon.match.func));
    expect(warnSpy.calledWith(mError)).to.be.true;
  });
});

带有覆盖率报告的单元测试结果:

  main
Error: fake error
    at Context.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/58357977/index.spec.ts:1:3887)
    at callFnAsync (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:415:21)
    at Test.Runnable.run (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:357:7)
    at Runner.runTest (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:535:10)
    at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:653:12
    at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:447:14)
    at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:457:7
    at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:362:14)
    at Immediate._onImmediate (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:425:5)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
    ✓ should safeLoad error (46ms)


  1 passing (55ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |       75 |      100 |                   |
 index.spec.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |      100 |      100 |       60 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/58357977

© www.soinside.com 2019 - 2024. All rights reserved.