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

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
    +“)”

最佳答案

试试这个...对我有用

 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()})`
    }
  }

关于javascript - 应该处理toString-toString方法应该返回正确的层次结构(无回复),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52582888/

10-12 15:01