在这里,我正在尝试为restPassword
更新单元测试用例。在这种方法中,我以post()
方法传递有效的文档和响应。我已经模拟了post
并试图在运行此测试用例时检查expect(spyPost).toHaveBeenCalledWith(document.save())
,但是由于no of calls: 0
,测试用例失败了。
有人能建议我这个测试用例有什么问题吗?
重置密码:
resetSendPasscode = async (request: Request, response: Response, entityType: EntityType): Promise<void> => {
tryCatch(async () => {
// query parameter validation
if (!request.query.email) {
throwError(e.invalidQueryParameters);
}
let model, emailKey, passwordResetKey;
if (entityType === EntityType.User) {
model = this.userModel;
emailKey = 'user_email';
passwordResetKey = 'user_password_reset';
} else {
model = this.vendorModel;
emailKey = 'vendor_email';
passwordResetKey = 'vendor_password_reset';
}
// db validation
const document = await model.findOne({ [emailKey]: request.query.email }, false);
if (!document) {
throwError(e.notFound);
}
const changeRequest = (document as any)[passwordResetKey];
// check if password request is made more than 3 attempts within 24 hours
if (
changeRequest &&
changeRequest?.attempts >= 3 &&
changeRequest.last_attempt_date &&
this.passwordResetUtility.timeDiff(changeRequest.last_attempt_date as Date).getHours() <= 24
) {
throwError(e.notAcceptable);
}
// create password reset token
const reset = this.passwordResetUtility.createResetToken(changeRequest);
// update datebase
(document as any)[passwordResetKey] = reset;
return post((document as Document).save())(response, [`${passwordResetKey}.status`]);
})(response);
};
发布方式:
static post = (funcCall: Promise<Document>) => async (response: Response, fields?: string[]) => {
try {
const dbResponse = await funcCall;
success(pick(dbResponse, fields ? ['_id', ...fields] : ['_id']), 201)(response);
} catch (error) {
throwError(error);
}
};
单元测试用例:
it('should call controller utility post method and pass promise of document save method.', async () => {
// Preparing
const request: Partial<Request> = {
query: {
email: 'user@1234.com',
},
};
const date = new Date('2020-07-08T10:34:15.239Z');
const mockFindOneReturn = {
_id: '5f0076b7bd530928fc0c0285',
user_no: 'USR00005',
user_full_name: 'user',
user_email: 'user@1234.com',
user_password: '$2b$10$kGJrsrOmMqfwUYh25a0g.O/SDoD9GA0AF69iYlvysSlbTplAh8YBW',
user_phone: '0123456785',
user_visits: 1,
user_permission_groups: ['5f0073c8bd530928fc0c0280'],
user_created_date: '2020-07-08T10:34:15.239Z',
user_status_is_active: true,
user_password_reset: {
password_reset_status_is_active: true,
key: 1234,
status: 'pending',
attempts: 0,
last_attempt_date: null,
password_reset_created_date: date,
password_reset_modified_date: '2020-07-08T10:34:15.239Z',
},
};
const documents = {
user_status_is_active: true,
user_visits: 0,
user_permission_groups: ['5f0073bcbd530928fc0c027f'],
_id: '5f85229f30329929af2d78d4',
user_full_name: 'sudharsan',
user_email: 'sudhar922@gmail.com',
user_phone: '9047060000',
user_created_date: '2020-07-08T10:34:15.239Z',
user_modified_date: '2020-10-13T06:17:56.456Z',
user_no: 'USR00014',
user_password_reset: {
password_reset_status_is_active: true,
key: 7314,
status: 'pending',
attempts: 0,
last_attempt_date: null,
},
};
const spyFindOne = jest.spyOn(userModel.getModel(), 'findOne').mockReturnValueOnce(mockFindOneReturn as any);
const spyPost = jest.spyOn(ControllerUtility, 'post').mockImplementation(() => async () => {});
const testModel = container.resolve(UserModel);
const modelPrototype = testModel.getModel();
const document = new modelPrototype(documents);
// Verifying
await userController.resetSendPasscode(request as Request, response as Response);
expect(spyFindOne).toHaveBeenCalledWith({ user_email: 'user@1234.com' }, false);
expect(spyPost).toHaveBeenCalledWith(document.save());
});