使用 proxyquire 和 sinon.js

如何解决使用 proxyquire 和 sinon.js

我正在尝试使用 sinon.js 和 getJSON 存根 jquery 的 donefailproxyquire 函数,但我总是收到一个错误,即 .getJSON,done并且失败不是功能

  const $ = require("jquery");
    const Progress = (() => {
      const getData = () => {
        return $.getJSON();
      };
      const showProgressBars = () => {
        getData()
          .done(function (data) {
            console.log(data);
          })
          .fail(function (e) {
            console.log("error getting data.",e);
          });
      };
      return {
          getData,showProgressBars
      }
    })();
module.exports=Progress;

测试

const proxyquire=require("proxyquire");
const sinon=require("sinon");
const expect=require("chai").expect;
describe("Progress",() => {
  it("should call getJSON",(done) => {
    const getJSONStub = sinon.stub();
    const jquerySpy = sinon.stub().callsFake(() => {
      return {
        getJSON: getJSONStub,};
    });
    const { getData } = proxyquire("../src/demo",{
      jquery: jquerySpy,});
    getData();
    expect(getJSONStub.callCount).to.be.eq(1);
    done();
  });
});

我的要求是测试这两个功能,但我无法做到这一点。我已经花了两天时间,但无法存根 getJSON、done 和 fail 函数

非常感谢任何帮助

解决方法

使用 sinon.stub() 存根 $.getJSON() 方法及其返回值 JQuery.jqXHR,它具有 .done().fail() 回调。您可以使用 .callsFake() 方法获取传入的原始回调,获取它们后,使用模拟数据或错误手动调用这些回调。

您不需要使用 proxyquire 包。

例如

index.js

const $ = require('jquery');

const Progress = (() => {
  const getData = () => {
    return $.getJSON();
  };
  const showProgressBars = () => {
    getData()
      .done(function (data) {
        console.log(data);
      })
      .fail(function (e) {
        console.log('error getting data.',e);
      });
  };
  return {
    getData,showProgressBars,};
})();

module.exports = Progress;

index.test.js

const sinon = require('sinon');
const expect = require('chai').expect;
const $ = require('jquery');
const { getData,showProgressBars } = require('./');

describe('Progress',() => {
  afterEach(() => {
    sinon.restore();
  });
  it('should call getJSON',async () => {
    const data = 'teresa teng';
    const getJSONStub = sinon.stub($,'getJSON').resolves(data);
    const res = await getData();
    expect(res).to.be.equal('teresa teng');
    sinon.assert.calledOnce(getJSONStub);
  });

  it('should get data success',() => {
    const data = 'teresa teng';
    const mJqXHR = {
      done: sinon.stub().callsFake(function (callback) {
        callback(data);
        return this;
      }),fail: sinon.stub(),};
    const getJSONStub = sinon.stub($,'getJSON').returns(mJqXHR);
    showProgressBars();
    sinon.assert.calledOnce(getJSONStub);
    sinon.assert.calledWithExactly(mJqXHR.done,sinon.match.func);
  });

  it('should handle error if get data fail',() => {
    const mErr = new Error('network');
    const mJqXHR = {
      done: sinon.stub().returnsThis(),fail: sinon.stub().callsFake(function (callback) {
        callback(mErr);
      }),'getJSON').returns(mJqXHR);
    showProgressBars();
    sinon.assert.calledOnce(getJSONStub);
    sinon.assert.calledWithExactly(mJqXHR.fail,sinon.match.func);
  });
});

单元测试结果:

  Progress
    ✓ should call getJSON
teresa teng
    ✓ should get data success
error getting data. Error: network
    at Context.<anonymous> (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/67267452/index.test.js:34:18)
    at callFn (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runnable.js:364:21)
    at Test.Runnable.run (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runnable.js:352:5)
    at Runner.runTest (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:677:10)
    at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:801:12
    at next (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:594:14)
    at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:604:7
    at next (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:486:14)
    at Immediate._onImmediate (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:572:5)
    at processImmediate (internal/timers.js:461:21)
    ✓ should handle error if get data fail


  3 passing (12ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.js |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?