繁体   English   中英

如何使用 sinon.js 模拟导出的 function?

[英]how to mock an exported function with sinon.js?

所以我有一个名为article.js的文件,如下所示:

import a from 'indefinite';

function formatArticle(value) {
  return a(value);
}

export { formatArticle };
export default formatArticle;

还有一个名为index.js的文件,如下所示:

import formatArticle from './article';

const format = (value, incomingFormat) => {
  if (incomingFormat === 'indefinite') {
    return formatArticle(value);
  }

  return value;
};

export default format;

我正在尝试测试我正在导入的 formatArticle function 是否在应该的时候被调用。 我为此使用sinonchai 这是测试文件:

import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';

import format from './index';
import * as article from './article';

chai.use(sinonChai);

describe('format', () => {
    describe('article', () => {
        describe(`when incoming format is 'indefinite'`, () => {
            const value = 'some value';
            const indefinite = 'indefinite';

            let formatArticleSpy;

            beforeEach(() => {
                formatArticleSpy = sinon.spy(article, 'formatArticle');
                format(value, indefinite);
            });

            it('should call formatArticle', () => {
                expect(formatArticleSpy).to.have.been.calledWith(value);
            });
        });
    });
});

但是,每当我运行测试套件时,它都会告诉我:

AssertionError: expected formatArticle to have been called with arguments some value

我在这个设置中做错了什么? 使用间谍是尝试这样做的正确方法吗? 我也尝试过使用 sinon.stub 并且得到相同的结果。

任何帮助表示赞赏!

sinon.js 使用Link Seams 来存根独立的 function。 formatArticle适合您的情况。

例如

article.js

import a from './indefinite';

function formatArticle(value) {
  return a(value);
}

export default formatArticle;

indefinite.js

export default function a(value) {
  return 'real data';
}

index.js

import formatArticle from './article';

const format = (value, incomingFormat) => {
  if (incomingFormat === 'indefinite') {
    return formatArticle(value);
  }

  return value;
};

export default format;

index.test.js

import proxyquire from 'proxyquire';
import sinon from 'sinon';

describe('61104001', () => {
  it('should pass', () => {
    const value = 'some value';
    const indefinite = 'indefinite';
    const formatArticleStub = sinon.stub().returns('fake data');
    const format = proxyquire('./', {
      './article': { default: formatArticleStub },
    }).default;
    const actual = format(value, indefinite);
    sinon.assert.match(actual, 'fake data');
    sinon.assert.calledWithExactly(formatArticleStub, value);
  });
  // you can do the rest
});

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

  61104001
    ✓ should pass (2259ms)


  1 passing (2s)

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   72.73 |       50 |   33.33 |   72.73 |                   
 article.ts    |   66.67 |      100 |       0 |   66.67 | 4                 
 indefinite.ts |      50 |      100 |       0 |      50 | 2                 
 index.ts      |   83.33 |       50 |     100 |   83.33 | 8                 
---------------|---------|----------|---------|---------|-------------------

源代码: https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/61104001

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM