should handle toString -The toString method should return the correct hierarchy (no reply)

前端 未结 2 1389
盖世英雄少女心
盖世英雄少女心 2021-01-26 18:07

I\'m working on a test and a lot of unit(hidden) tests were given , however I ran into this error with a block of my code. Can you all please help me out?

getStr         


        
相关标签:
2条回答
  • 2021-01-26 18:49

    I guess your test fails because you mix template literals and string concatenation, if you take e.g.:

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

    then a message will be inserted by the template:

    `Heureka! + "by" Archimedes`
    

    I guess it should rather be:

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

    Also repliedTo.authorName should be wrapped in ${...}

    0 讨论(0)
  • 2021-01-26 19:00

    Try this... it worked for me

     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()})`
        }
      }
    
    0 讨论(0)
提交回复
热议问题