简体   繁体   English

用 sinon 调用 yield* function

[英]stubbing a yield* function call with sinon

I'm new to function* / yield / yield* as well as to unit testing, and I'm not sure why my unit test code isn't working.我是function* / yield / yield*以及单元测试的新手,我不确定为什么我的单元测试代码不起作用。 It's using sinon stubbing with mocha testing framework.它使用 sinon stubbing 和 mocha 测试框架。 I've read up on function* / yield / yield* but it's still confusing to me.我已经阅读了function* / yield / yield*但它仍然让我感到困惑。

Using the Co library, I have a function* with a yield* inside that calls another function* .使用 Co 库,我有一个function*里面有一个yield*调用另一个function* I'm trying to mock the call to the function* called by yield* with a sinon stub, but the stub returns undefined.我试图用 sinon 存根模拟对yield*调用的function*的调用,但存根返回未定义。 The stub returns the correct response if it's simply yield and not yield* .如果存根只是yield而不是yield* ,则它会返回正确的响应。

import:进口:

import * as name from './file';

call to original generator function:调用原始生成器 function:

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

function secondFunc: function 第二功能:

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

unit test:单元测试:

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);     

(Of note, the original code is not something I wrote. I'm just adding unit tests.) (注意,原始代码不是我写的。我只是添加单元测试。)

It can be slightly simpler by stubbing with SinonStub#callsFake通过使用SinonStub#callsFake存根可以稍微简单一些

const nameGetStub = sinon.stub(name, "get").callsFake(function * () {
  yield 'fake name'
});

Async generator can also be faked simply by making is async function *异步生成器也可以简单地通过 is async function *

Here is the unit test solution for co module used with yield* [expression] .这是与yield* [expression]一起使用的co模块的单元测试解决方案。

file.ts : file.ts

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

index.ts : 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 : 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");
    });
  });
});

Unit test result with coverage report:带有覆盖率报告的单元测试结果:

 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 |
---------------|----------|----------|----------|----------|-------------------|

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

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

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