使用sinon截断yield *函数调用

时间:2019-12-12 16:06:41

标签: javascript unit-testing mocha sinon yield

我是function* / yield / yield*以及单元测试的新手,我不确定为什么我的单元测试代码无法正常工作。它使用带有Mocha测试框架的sinon存根。我已经读过function* / yield / yield*,但仍然让我感到困惑。

使用Co库,我有一个function*,里面有一个yield*,它调用了另一个function*。我正在尝试使用sinon存根模拟对function*调用的yield*的调用,但是存根返回未定义的。如果存根只是yield而不是yield*,则存根返回正确的响应。

导入:

import * as name from './file';

调用原始生成器函数:

export const func = (a, b, c) => co(secondFunc.bind(this, a, b, c));

功能secondFunc:

function* secondFunc(a, b, c) {
  try {
    const x = yield* name.get(a); // this is where x is undefined
    // logic
    return value;
  } catch (err) {
    // logic
  }
}

单元测试:

const callback = sinon.stub(name, 'get');
callback.returns(new Promise((resolved, reject) => resolved(response)));

co(func("a", "b", "c")).then((value) => {
    console.log(value);
    done();
}).catch(done);     

(请注意,原始代码不是我写的。我只是添加单元测试。)

1 个答案:

答案 0 :(得分:0)

这是与yield* [expression]一起使用的file.ts模块的单元测试解决方案。

export function* get(a) { yield "real data"; }

index.ts

import * as name from "./file"; import co from "co"; export function* secondFunc(a, b, c) { try { const x = yield* name.get(a); return x; } catch (err) { console.error(err); } } export const func = (a, b, c) => co(secondFunc.bind(null, a, b, c));

index.spec.ts

import { secondFunc, func } from "./"; import { expect } from "chai"; import * as name from "./file"; import sinon from "sinon"; import co from "co"; describe("59308604", () => { afterEach(() => { sinon.restore(); }); it("should pass secondFunc", () => { function* mGen() { yield "fake data"; } const nameGetStub = sinon.stub(name, "get").returns(mGen()); const gen = secondFunc("a", "b", "c"); expect(gen.next().value).to.be.eql("fake data"); expect(gen.next()).to.be.eql({ value: undefined, done: true }); sinon.assert.calledWith(nameGetStub, "a"); }); it("should pass func", async () => { function* mGen() { return "fake data"; } const nameGetStub = sinon.stub(name, "get").returns(mGen() as any); const actual = await func("a", "b", "c"); expect(actual).to.be.equal("fake data"); sinon.assert.calledWith(nameGetStub, "a"); }); it("test co", () => { function* g1() { return "123"; } return co(function*() { var result = yield* g1(); return result; }).then((value) => { expect(value).to.be.eql("123"); }); }); });

 59308604
    ✓ should pass secondFunc
    ✓ should pass func
    ✓ test co


  3 passing (17ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |    95.12 |    66.67 |    92.31 |    94.44 |                   |
 file.ts       |       50 |        0 |        0 |       50 |                 2 |
 index.spec.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |    88.89 |      100 |      100 |    85.71 |                 9 |
---------------|----------|----------|----------|----------|-------------------|

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

svg g { fill: #d4d5d9 }

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

相关问题