typescript - 测试异步函数不起作用
问题描述
上周我开始学习 TypeScript,我有 4 年的 Java 编程经验。变化挺大的。无论如何,我遇到了一个过去几个小时都无法解决的问题。
我有一个架构:
const userSchema = new Schema({
username : { type: String, required: true },
passwordHash : { type: String, required: true },
passwordSalt : { type: String, required: true },
email : { type: String, required: true },
emailVerified : { type: Boolean, default: false },
firstName : { type: String, required: true },
lastName : { type: String, required: true },
postUpvotes : [Schema.Types.ObjectId],
postDownvotes : [Schema.Types.ObjectId],
commentUpvotes : [Schema.Types.ObjectId],
commentDownvotes : [Schema.Types.ObjectId],
creationDate : { type: Date, default: Date.now }, };
现在在我的测试中,我想测试我的必填字段,这意味着测试我可以在我的 mongodb 中插入一个对象。
describe('Test required user fields', function(){
const gandalfTheWizard = new Student();
it('Test required username', function() {
gandalfTheWizard.username = 'cooolboy';
return gandalfTheWizard.save().then(() => Promise.reject(new Error('Expected method to reject.')),
err => { return expect(err).to.be.instanceOf(Error) }
);
});
it('Test required passwordHash', function() {
gandalfTheWizard.passwordHash = 'tes1';
return gandalfTheWizard.save().then(() => Promise.reject(new Error('Expected method to reject.')),
err => { return expect(err).to.be.instanceOf(Error) }
);
});
it('Test required passwordSalt', function() {
gandalfTheWizard.passwordSalt = 'tes2';
return gandalfTheWizard.save().then(() => Promise.reject(new Error('Expected method to reject.')),
err => { return expect(err).to.be.instanceOf(Error) }
);
});
it('Test required email', function() {
gandalfTheWizard.email = 'tes3';
return gandalfTheWizard.save().then(() => Promise.reject(new Error('Expected method to reject.')),
err => { return expect(err).to.be.instanceOf(Error) }
);
});
it('Test required firstName', function() {
gandalfTheWizard.firstName = 'tes4';
return gandalfTheWizard.save().then(() => Promise.reject(new Error('Expected method to reject.')),
err => { return expect(err).to.be.instanceOf(Error) }
);
});
it('Test required lastName - inserts correct object', function() {
gandalfTheWizard.lastName = 'tes5';
// i spent multiple hours trying to figure out why this exact test always fails on first time
// i can not figure it out and i am kinda done trying to figure this out
// insertOne() buffering timed out after 10000ms - is the underlying error
this.timeout(50000);
return gandalfTheWizard.save();
});
});
我还有一些其他测试,其中我只是保存了一个对象,它通过了:
it('Save Student in database', function () {
Student.findOne({ firstName: 'Harry' }).then(result => {
if (result != null) {
expect(result).to.be.null;
}
});
timeWizardHarry.save();
Student.findOne().then(result => {
if (result != null) {
expect(result._id).to.exist;
expect(result.username).to.equal(timeWizardHarry.username);
expect(result.email).to.equal(timeWizardHarry.email);
}
});
});
所以我有点没有想法,希望有人可以帮助我完成这件事。抱歉,如果您需要更多信息。
解决方案
我遇到的问题是我实际上并没有连接到我的数据库。我的测试完全错误,我的数据库设置也是如此。我忘了使用 AWAIT 我有
import * as mongoose from 'mongoose'
这是行不通的。
import mongoose from 'mongoose'
让它工作。
推荐阅读
- javascript - onChange 在 reactjs antd 中使用 setState 时返回一个回调
- apache - 如何用问号重定向链接
- android - 在后台Android中进行繁重处理时UI冻结
- reactjs - 使用来自反应应用程序的 axios 将数据传递给 Web 服务
- python - 如果引发异常,则删除 Django 模型实例
- react-native - 世博联系原生模式
- android - 我的代码抛出错误 [Fatal error: Main] 你能告诉我为什么吗?
- excel - 是否有任何代码可以识别特定单元格(b2)为真,然后将公式替换为另一个单元格(i2)?
- objective-c - 使用目标 c 将字符串转换为 ByteArray
- linux - 虚拟主机的 crontab