node.js - 使用依赖注入(inject)在 Typescript 中进行单元测试

标签 node.js unit-testing typescript dependency-injection sinon

我在单元测试期间遇到了 typescript 问题。我尝试像下面的代码示例一样对模块进行单元测试:

import {Express} from 'express'

export interface BootClassInterface {
  setup(): Express
}

export class BootClass implements BootClassInterface {

  constructor(private express: () => Express ){

  }

  public test(){
      const app = this.express()
      app.get("/test", (req, res) => {
        res.send("Hello World")
      })

      return app;
  }
}

为了测试建议,我想知道是否调用了 express().get() 以及第一个参数是否为“/test”。在我切换到 typescript 之前,我总是使用模块 sinonJS监视或 stub 功能,以便我可以测试是否正确使用了某个依赖项。现在使用 Typescript,我遇到了我在模块中设置的严格类型的问题。例如:

import * as chai from 'chai'
import 'mocha'
import * as sinon from 'sinon'
import * as express from 'express'

import * as Boot from '../../src/Core/Boot'

const assert = chai.assert

suite('[CORE] unit test /Core/Boot.ts', () => {
  let BootClass: Boot.BootClassInterface

  setup(() => {
    const expressApp = () => {
      const app = express()
      app.get = sinon.spy()
      return app
    }

    const Services: Boot.BootServicesInterface = { express: expressApp }
    BootClass = new Boot.BootClass(Services)

  })

  test('Do first test', () => {
    const app = BootClass.setup()
    chai.assert.equal(app.get.called, 1)
  })
})

上面的代码示例导致以下 Typescript 编译错误:

error TS2339: Property 'called' does not exist on type 'ApplicationRequestHandler<Express>'.

我明白为什么 typescript 会返回这个错误,而且它在某种程度上什至是显而​​易见的。我什至知道一个可能的解决方案,我在模块中接受 Express。

但我正在寻找一种更优雅的方式来监视/ stub /模拟我的依赖项以进行测试建议,但同时具有严格类型化的优势。

最佳答案

如您所述,您将 Express 指定为 BootClassInterface 接口(interface)的返回类型。因此 app 将被视为 Express 并且它将查找其属性而不是您的模拟。

您也可以只将 app 的一个属性转换为 any。尝试:

 chai.assert.equal((<any>app.get).called, 1)

或者使用 Sinon 类型定义:

 chai.assert.equal((<SinonSpy>app.get).called, 1)

关于node.js - 使用依赖注入(inject)在 Typescript 中进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616329/

相关文章:

javascript - 使用 node-mysql 同时查询

java - eclipse 第谷 : Testing plug-in's without using single test-bundles

javascript - 监视 Jasmine 中另一个函数调用的函数

node.js - Nodejs、TypeScript、ts-node-dev 和顶级等待

node.js - 环回 hasandbelongstomany 关系表不存在

php - 检查进程是否正在运行,如果没有则启动它

node.js - 如果我们将nodejs进程放入docker容器中,会对性能产生什么影响?

java - 单元测试resteasy时未注入(inject)@Context对象

typescript - 是否可以使用 webpack 加载程序从文件生成 Typescript 接口(interface)?

javascript - Typescript .bind() 在使用超过 4 个参数时给出类型错误