假设TypeScript中有以下类:
class MongoDbContext implements IMongoDbContext {
private connectionString : string;
private databaseName : string;
private database : Db;
public constructor (connectionString : string, databaseName : string) {
this.connectionString = connectionString;
this.databaseName = databaseName;
}
public async initializeAsync () : Promise<MongoDbContext> {
// Create a client that represents a connection with the 'MongoDB' server and get a reference to the database.
var client = await MongoClient.connect(this.connectionString, { useNewUrlParser: true });
this.database = await client.db(this.databaseName);
return this;
}
}现在,我想测试当我试图连接到一个不存在的MongoDB服务器时是否抛出了异常,这是通过以下集成测试完成的:
it('Throws when a connection to the database server could not be made.', async () => {
// Arrange.
var exceptionThrowed : boolean = false;
var mongoDbContext = new MongoDbContext('mongodb://127.0.0.1:20000/', 'databaseName');
// Act.
try { await mongoDbContext.initializeAsync(); }
catch (error) { exceptionThrowed = true; }
finally {
// Assert.
expect(exceptionThrowed).to.be.true;
}
}).timeout(5000);当我运行这个单元测试时,我的CMD窗口不打印摘要。它好像挂在什么地方。
在这种情况下,我做错了什么?
致以亲切的问候,
发布于 2018-07-05 20:13:58
我设法找到了问题所在。似乎我必须关闭我的'MongoClient‘连接才能正确退出。
因此,我添加了一个额外的方法
public async closeAsync () : Promise<void> {
await this.client.close();
}此方法在每次测试后调用。
https://stackoverflow.com/questions/51189956
复制相似问题