2017-04-25 85 views
3

单元测试期间,我遇到了打字稿的问题。我试图单元测试的模块,如下面的代码示例:使用依赖注入进行单元测试

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

为了测试提出我想知道,如果快递()得到()被调用,如果第一个参数是“/测试”。在我切换到typescript之前,我总是使用模块sinonJS来窥探或存根功能,以便我可以正确使用某个依赖项的测试。现在使用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) 
    }) 
}) 

上述结果在下面的打字稿编译错误的代码示例:

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

我明白为什么打字稿回报这个错误,不知何故,甚至是明显的。我甚至知道一个可能的解决方案,我接受模块中的任何Express。

但我正在寻找一种更优雅的方式能够间谍/存根/模拟我的依赖测试建议,但同时具有严格打字的优点。

+0

另外,在单元测试你为什么_create_整个快递应用程序? – atoth

回答

1

如您所述,您指定Express作为BootClassInterface接口上的返回类型。因此app将被视为Express,它会查找它的属性而不是你的模拟。

您也可以只投一个app的房产any。尝试:

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

或者使用兴农类型定义:

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