我在单元测试期间遇到了 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/