javascript - Sinon如何对异步函数进行单元测试的 stub 方法

标签 javascript unit-testing ember.js sinon sinon-chai

我正在尝试使用 mocha 和 sinon.js 为异步函数编写单元测试

下面是我的测试用例

  describe('getOperations', function () {
    let customObj, store, someObj
    beforeEach(function () {
      someObj = {
        id: '-2462813529277062688'
      }
      store = {
        peekRecord: sandbox.stub().returns(someObj)
      }
    })
    it('should be contain obj and its ID', function () {
      const obj = getOperations(customObj, store)
      expect(obj).to.eql(someObj)
    })
  })

下面是我正在测试的异步函数的定义。

async function getOperations (customObj, store) {
  const obj = foo(topLevelcustomObj, store)
  return obj
}

function foo (topLevelcustomObj, store) {
    return store.peekRecord('obj', 12345)
}

测试用例失败,因为返回的 promise 被一条消息拒绝

TypeError: store.query is not a function at Object._callee$.

我正在测试的代码没有在任何地方调用store.query,而且我也对store.peekRecord进行了 stub ,因此不确定它是如何被调用的。

最佳答案

您的 getOperations 函数使用 async 语法,因此您需要在测试用例中使用 async/await 。而且,它工作得很好。

例如 index.ts

export async function getOperations(customObj, store) {
  const obj = foo(customObj, store);
  return obj;
}

export function foo(customObj, store) {
  return store.peekRecord("obj", 12345);
}

index.test.ts:

import { getOperations } from "./";
import sinon from "sinon";
import { expect } from "chai";

describe("59639661", () => {
  describe("#getOperations", () => {
    let customObj, store, someObj;
    beforeEach(function() {
      someObj = {
        id: "-2462813529277062688",
      };
      store = {
        peekRecord: sinon.stub().returns(someObj),
      };
    });
    it("should pass", async () => {
      const obj = await getOperations(customObj, store);
      expect(obj).to.deep.eq(someObj);
    });
  });
});

100%覆盖率的单元测试结果:

  59639661
    #getOperations
      ✓ should pass


  1 passing (14ms)

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

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

关于javascript - Sinon如何对异步函数进行单元测试的 stub 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59639661/

相关文章:

ember.js - Ember data store.find() 在使用无效 ID 调用时创建空项目

javascript - 如何用先前计算的变量替换变量的值

javascript - 如何在屏幕中间垂直对齐内容?

java - 如何使用 mockito 测试 REST 服务?

c# - 单元测试会不必要地混淆代码,还是有更好的方法?

javascript - 如何在 ember.js 中重用/干燥这些路由

javascript - 如何在 Ember CLI 结构中的 EmberJS 主页上设置 “Loading Data from a Server” 示例?

javascript - 从 Google 表格创建 JSON 对象

javascript - 允许下拉元素溢出已隐藏或滚动溢出的容器

python - 我如何使这个测试通过?