Recently, I made the switch from Java to TypeScript and encountered a challenging problem that has been occupying my time for hours.
Here is the schema that I am working with:
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 }, };
My current focus is on testing the required fields in my MongoDB database during tests. In order to do this effectively, I have set up specific test cases.
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) }
);
});
// More test cases for other required fields
it('Test required lastName - inserts correct object', function() {
gandalfTheWizard.lastName = 'tes5';
this.timeout(50000);
return gandalfTheWizard.save();
});
});
In addition to these specific test cases, I also have tests where I simply save an object, which passes successfully:
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);
}
});
});
I am currently stuck and hoping someone can provide some insight or assistance on this issue. Any help is greatly appreciated. Thank you.