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



我正在做一个测试,并给出了很多单元(隐藏(测试,但是我在代码块中遇到了此错误。你们能帮帮我吗?

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 方法应返回正确的层次结构(无回复(

我打算遵循以下格式:没有回复: 消息 + " by " + author.name

回复: 消息 + " by " + author.name + " (回复" + repliedTo.author.name + ")">

我想你的测试失败了,因为你混合了模板文字和字符串连接,如果你采取例如:

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

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

`Heureka! + "by" Archimedes`

我想它应该是:

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

此外,repliedTo.authorName也应用${...}包裹

试试这个...它对我有用

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

最新更新