angular6 - 无法在单元测试中验证 JSON 的结构,并且结果与预期结果不匹配
问题描述
我正在尝试运行一个在收到unit test
响应时调用的函数。http.post
handleSuccessResponseForUserProfileRequest(res: HttpSentEvent|HttpHeaderResponse|HttpResponse<any>|HttpProgressEvent|HttpUserEvent<any>) {
const ev = <HttpEvent<any>>(res);
if (ev.type === HttpEventType.Response) {
console.log('response from server: body ',ev.body);
const isResponseStructureOK: boolean = this.helper.validateServerResponseStructure(ev.body);
if (isResponseStructureOK) {
const response: ServerResponseAPI = ev.body;
this.userProfileSubject.next(new Result(response.result, response['additional-info']));
} else {
this.userProfileSubject.next(new Result('error', 'Invalid response structure from server'));
}
} else {
}
}
validateServerResponseStructure
检查响应的结构是否正常。它应该有result
和additional-info
键
validateServerResponseStructure(res: any): boolean {
const keys = Object.keys(res);
const isTypeCorrect: boolean = (
['result', 'additional-info'].every(key => keys.includes(key))
&& keys.length === 2);
return isTypeCorrect;
}
我写的单位案例是
fit('should return if user information isn\'t stored', () => {
const body = JSON.stringify({"result":"success", "additional-info":"some additional info"});
const receivedHttpEvent = new HttpResponse({body:body});
const userService: UserManagementService = TestBed.get(UserManagementService);
const helperService:HelperService = TestBed.get(HelperService);
spyOn(userService['userProfileSubject'],'next');
//spyOn(helperService,'validateServerResponseStructure').and.returnValue(true);
userService.handleSuccessResponseForUserProfileRequest(receivedHttpEvent);
const expectedResult = new Result('success', 'some additional info');
expect(userService['userProfileSubject'].next).toHaveBeenCalledWith(expectedResult);
});
如果我不这样做spy
,validateServerResponseStructure
那么我的测试用例会失败,因为validateServerResponseStructure
即使对我来说它看起来结构还可以。
Expected spy next to have been called with [ Result({ result: 'success', additionalInfo: 'some additional info' }) ] but actual calls were [ Result({ result: 'error', additionalInfo: 'Invalid response structure from server' }) ].
如果我监视validateServerResponseStructure
并返回,true
那么我会收到错误
Expected spy next to have been called with [ Result({ result: 'success', additionalInfo: 'some additional info' }) ] but actual calls were [ Result({ result: undefined, additionalInfo: undefined }) ].
有趣的是,如果我添加以下两个打印件,那么它们会显示不同的值!!
console.log('extracted response ',response);
console.log('sending response '+response.result + 'and additional info' +response['additional-info']);
我懂了
extracted response {"result":"success","additional-info":"some additional info"}
sending response undefined and additional info undefined
我究竟做错了什么?
解决方案
有趣的是,如果我将单元测试中的 in 更改为type
<T>
而不是.httpResponse
Object
string
const body = {"result":"success", "additional-info":"some additional info"};
const receivedHttpEvent = new HttpResponse({body:body});
推荐阅读
- python - Windows 命令找不到脚本
- html - 如何强制元素出现在父级及其父级后面?
- python - Discord.py - 有没有办法使用 @commands.has_role('RoleName') 来检查这个人是否具有两个角色之一?
- php - Snipe-IT:电子邮件通知 URL 链接已损坏(指向 localhost)
- neo4j - 简化密码,匹配所有节点和两种节点类型之间的关系
- module - 在 prestashop 中安装模块时出现错误
- github - GitHub 使用什么库来呈现 Markdown 文档之间的差异?
- node.js - 如何在单独的 nodejs 脚本中使用 create-react-app 中的源文件?
- numpy - 使用 linspace 计算分母时如何防止获得较大的值?
- bash - 在 BASH 中,如何保存要在其余脚本完成后运行的命令?