应该处理toString-toString方法应该返回正确的层次结构(无回复)

时间:2018-09-30 23:11:27

标签: javascript

我正在进行测试,并且进行了许多单元(隐藏)测试,但是我在代码块中遇到了此错误。大家可以帮我吗?

getString(comment) {
const authorName = comment.getAuthor().getName();
if (!comment.getRepliedTo()) return authorName;
return `${comment.getMessage()} by ${authorName} (replied to 
${this.getString(comment.getRepliedTo())})`;
}
toString() {
const authorName = this.getAuthor().getName();
if (!this.getRepliedTo()) {
return `${this.message} by ${authorName}`;
}
return this.getString(this);
}
}

错误提示:  应处理toString toString方法应返回正确的层次结构(无回复)

我应遵循以下格式:     没有回复:     邮件+“ by” + author.name

回复为:     邮件+“ by” + author.name +“(回复为” + repliedTo.author.name     +“)”

2 个答案:

答案 0 :(得分:1)

我猜您的测试失败了,因为您将模板文字和字符串串联在一起,例如:

 `${this._message} + "by" ${authorName}`

然后模板会插入一条消息:

`Heureka! + "by" Archimedes`

我想应该是:

`${this._message} by ${authorName}`

还应将repliedTo.authorName包装在${...}

答案 1 :(得分:0)

尝试一下...对我有用

 getString(comment) {
    const authorName = comment.getAuthor().getName();
    if (!comment.getRepliedTo()) return authorName;
    return `${comment.getMessage()} by ${authorName} (replied to ${this.getString(comment.getRepliedTo())})`;
  }
  toString() {
   if(!this._repliedTo){
      return `${this._message} by ${this._author.getName()}`;
    } else {
      return `${this._message} by ${this._author.getName()} (replied to ${this._repliedTo.getAuthor().getName()})`
    }
  }