繁体   English   中英

诗农存根不适用于导出功能

[英]Sinon stub not working on exported function

我试图存根一个重新加载网页的函数。

export function reloadPage() {
    window.location.reload();
}

这就是我对函数进行存根的方式:

import * as ref from 'file/where/reloadpage/is/defined';

describe('...', function () {
    ...
    before(function () {
       this.reloadStub = sinon.stub(ref, 'reloadPage');
    });
});

它仍然没有正确地存根该方法。 我的测试抛出“整页重新加载”错误。

我不知道我做错了什么。

这是一个工作示例:

index.ts

export function reloadPage() {
  console.log('reload');
  window.location.reload();
}

index.spec.ts

import * as ref from './';
import sinon from 'sinon';
import { expect } from 'chai';

describe('57519517', () => {
  let reloadStub;
  before(() => {
    reloadStub = sinon.stub(ref, 'reloadPage');
  });

  it('should mock reload page', () => {
    const logSpy = sinon.spy(console, 'log');
    ref.reloadPage();
    expect(reloadStub.calledOnce).to.be.true;
    expect(logSpy.notCalled).to.be.true;
  });
});

单元测试结果:

 57519517
    ✓ should mock reload page


  1 passing (12ms)

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

暂无
暂无

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

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