首页 > 解决方案 > 测试异步函数不起作用

问题描述

上周我开始学习 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);
        }
    });
});

所以我有点没有想法,希望有人可以帮助我完成这件事。抱歉,如果您需要更多信息。

标签: typescriptmocha.jschai

解决方案


我遇到的问题是我实际上并没有连接到我的数据库。我的测试完全错误,我的数据库设置也是如此。我忘了使用 AWAIT 我有

import * as mongoose from 'mongoose' 

这是行不通的。

import mongoose from 'mongoose' 

让它工作。


推荐阅读