【发布时间】: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