首页 > 解决方案 > 比较 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一个类的一般和对象之间的混合体,但我不知道我在哪里犯了错误。

标签: typescriptangular6

解决方案


似乎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


推荐阅读