【问题标题】:Error in comparing two object in Angular/Typescript比较 Angular/Typescript 中的两个对象时出错
【发布时间】:2019-09-17 09:02:02
【问题描述】:

我的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 和类对象之间的混合体,但我不知道我在哪里犯了错误。

【问题讨论】:

    标签: typescript angular6


    【解决方案1】:

    使用as 转换为Tag 似乎没有像我预期的那样工作。如果我明确创建 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);
    

    但我不明白为什么?很高兴接受另一个带有解释的答案

    我添加了一些调试打印来检查castedTagtempTag 的类型。我的结论是使用as 不会改变对象的底层类型。 as 只允许将对象视为其他类型。所以castedTag 仍然是Object,即使我使用的是as。如果我想要Tag,我需要做一个new Tag

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    相关资源
    最近更新 更多