typescript - 比较 Angular/Typescript 中的两个对象时出错
问题描述
我的Jasmine
测试中有以下变量
tag1:Tag;
tag2:Tag;
tag3:Tag;
supportedTagsArray:Array<Tag>;
supportedTags: SupportedTags;
它们是以下类的对象
export class Tag{
constructor(
public course:string,
public subject:string,
public topic:string
){}
}
export class SupportedTags{
'supported-tags':Array<Tag>;
constructor(
supportedTags:Array<Tag>
){
this['supported-tags'] = supportedTags;
}
}
我在我的以下方式初始化它们spec
this.tag1 = new Tag('c1','s1','t1');
this.tag2 = new Tag('c2','s2','t2');
this.tag3 = new Tag('c3','s3','t3');
this.supportedTagsArray = new Array<Tag>(this.tag1,this.tag2,this.tag3);
this.supportedTags = new SupportedTags(this.supportedTagsArray);
在 中spec
,我按以下方式比较值
expect(updateQuestionComponent.supportedTags["supported-tags"])
.toEqual(questionManagementService.supportedTags);
updateQuestionComponent.supportedTags
在我的Angular
.ts
班级中定义如下
supportedTags:SupportedTags;
并且它的值是在接收来自的数据时填充的Observable
。
let resultObject = JSON.parse(result.additionalInfo) as SupportedTags;
console.log('got tag object ',resultObject);
let tags:Tag[] = [];
console.log(resultObject["supported-tags"].length);
for(let count=0;count<resultObject["supported-tags"].length;count++)
{
console.log("count is ",count);
console.log("adding tag: ",resultObject["supported-tags"][count] as Tag);
tags.push(resultObject["supported-tags"][count] as Tag);
}
let supportedTags = new SupportedTags(tags);
this.supportedTags = supportedTags;
从 observable 接收到的数据是
Result {result: "success", additionalInfo: "{"supported-tags":[{"course":"c1","subject":"s1","…2"},{"course":"c3","subject":"s3","topic":"t3"}]}"}
additionalInfo: "{"supported-tags":[{"course":"c1","subject":"s1","topic":"t1"},{"course":"c2","subject":"s2","topic":"t2"},{"course":"c3","subject":"s3","topic":"t3"}]}"
result: "success"
我的测试用例失败并出现以下错误Expected [ Object({ course: 'c1', subject: 's1', topic: 't1' }), Object({ course: 'c2', subject: 's2', topic: 't2' }), Object({ course: 'c3', subject: 's3', topic: 't3' }) ] to equal SupportedTags({ supported-tags: [ Tag({ course: 'c1', subject: 's1', topic: 't1' }), Tag({ course: 'c2', subject: 's2', topic: 't2' }), Tag({ course: 'c3', subject: 's3', topic: 't3' }) ] }).
为什么?
更新比较中有一个错误。
expect(updateQuestionComponent.supportedTags["supported-tags"]).toEqual(questionManagementService.supportedTags);
应该
expect(updateQuestionComponent.supportedTags["supported-expect(updateQuestionComponent.supportedTags["supported-tags"]).toEqual(questionManagementService.supportedTags["supported-tags"]);
但修复后,我收到以下错误
Expected $[0] to be a kind of Tag, but was Object({ course: 'c1', subject: 's1', topic: 't1' }).
Expected $[1] to be a kind of Tag, but was Object({ course: 'c2', subject: 's2', topic: 't2' }).
Expected $[2] to be a kind of Tag, but was Object({ course: 'c3', subject: 's3', topic: 't3' }).
我想我是Object
一个类的一般和对象之间的混合体,但我不知道我在哪里犯了错误。
解决方案
似乎Tag
使用的铸造as
没有像我预期的那样工作。如果我明确创建实例,Tag
那么比较有效
console.log("count is ",count);
let castedTag = (resultObject["supported-tags"][count]) as Tag;
console.log("adding tag: ",castedTag);
let tempTag = new Tag(castedTag.course,castedTag.subject,castedTag.topic);
tags.push(tempTag);
但我不明白为什么?很高兴接受另一个带有解释的答案
我添加了一些调试打印来检查 和 的castedTag
类型tempTag
。我的结论是 usingas
不会改变对象的底层类型。as
只允许将对象视为其他类型。castedTag
即使我Object
使用的是as
. 如果我愿意Tag
,我需要做一个new Tag
。
推荐阅读
- javascript - 如何根据 id 使用另一个嵌套数组对嵌套数组对象进行排序?
- c# - 身份服务器 OpenID 配置 Http 重定向
- cross-browser - 如何为边缘应用样式?
- react-native - 返回时 react-native Redux 更新状态
- laravel - 如何在树多对多表之间创建关系?
- mysql - 在具有 3 个表的 mysql 中查找不匹配的记录
- mysql - MySQL中的模式VS表VS列CHARSET有什么区别?
- wordpress - 如何知道 WooCommerce REST API 中哪个产品变体具有哪个变体 ID
- db2 - 是否建议或可以同时在两个不同的物理文件上运行 RGZPFM?
- swift - 如何使用 Swift 格式化开头没有“0”的日期?