简体   繁体   English

检查 TypeScript 中 sinon 存根的参数

[英]Check arguments of sinon stub in TypeScript

I have a unit test that checks arguments of the function.我有一个单元测试来检查函数的参数。

it('Should return product from DB', () => {
  stub(ProductModel, 'findById').returns({
    lean: stub().returns({ total: 12 }),
  });


  getProduct(product_id);

  expect((ProductModel.findById as any).firstCall.args[0]).to.equal('product_id');
});

My question is: Is there any other better way to do it?我的问题是:还有其他更好的方法吗? I have to always cast to any to avoid getting error.我必须始终强制转换为any以避免出错。 I have also tried stubFunc.calledWith(args) , but in a result I get only true/false instead of expected/actual values.我也试过stubFunc.calledWith(args) ,但结果我只得到真/假而不是预期/实际值。

You can use Assertions API of sinon .您可以使用sinon断言 API Besides, the return value of sinon.stub() method is a sinon stub.此外, sinon.stub()方法的返回值是sinon存根。 So you can use this return value instead of using ProductModel.findById .因此,您可以使用此返回值而不是ProductModel.findById By doing this, you don't need to type cast to any explicitly.通过这样做,您不需要显式地将类型转换为any

Eg例如

index.ts : index.ts

import { ProductModel } from "./model";

function getProduct(id: string) {
  return ProductModel.findById(id).lean();
}

export { getProduct };

model.ts : model.ts

class ProductModel {
  public static findById(id: string): { lean: () => { total: number } } {
    return { lean: () => ({ total: 0 }) };
  }
}

export { ProductModel };

index.test.ts : index.test.ts

import { stub, assert } from "sinon";
import { getProduct } from "./";
import { ProductModel } from "./model";

describe("60034220", () => {
  it("should pass", () => {
    const product_id = "1";
    const leanStub = stub().returns({ total: 12 });
    const findByIdStub = stub(ProductModel, "findById").returns({
      lean: leanStub,
    });
    getProduct(product_id);
    assert.calledWithExactly(findByIdStub, product_id);
    assert.calledOnce(leanStub);
  });
});

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

60034220
    ✓ should pass


  1 passing (28ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |       90 |      100 |    66.67 |    94.74 |                   |
 index.test.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |      100 |      100 |      100 |      100 |                   |
 model.ts      |    66.67 |      100 |    33.33 |       80 |                 3 |
---------------|----------|----------|----------|----------|-------------------|

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

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

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