我正在研究Node,Express和TypeScript,这样做是为了建立一些测试。
要遵循最佳实践,SOLID等我已将路由器与控制器分离。
我有一个index.ts文件
proc1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)
# don't put wait here because of potential deadlock if stdout buffer gets full and we're waiting for proc2 to consume buffer then we're deadlocked
proc2 = subprocess.Popen(cmd2, stdin=proc1.stdout, stdout=self.fw, shell=True)
# ok to wait here
proc2.wait()
# ok to test return code after proc2 completes
if proc2.returncode != 0:
print('Error spawning cmd2')
else:
print('Success spawning cmd2')
我还有一个index.spec.ts文件
import { Router } from 'express';
import { TestController } from '../controllers/test.controller';
let testController = new TestController();
let index = Router();
/* GET home page. */
index.get('/', (req, res, next) => testController.get(req, res));
export default index;
当我尝试通过gulp运行测试时,我得到以下消息
import assert = require('assert');
import * as mocha from 'mocha';
import { index } from '../routes/index';
import { TestController } from '../controllers/test.controller';
import { Request, Response } from 'express';
var sinon = require('sinon');
var should = require('should');
import app from '../App';
describe('Indexrouter', function () {
describe('#Calling get /', function () {
it('should call test controller get function.', function () {
});
});
});
我可以运行服务器ok,它会生成带有响应的正确端点。虽然我确实得到了关于导出变量'index'的工作。似乎在测试中我无法导入索引模块。
我设法让控制器测试工作,整个项目可以看到here