Почему в моем тесте не обновляется commentCount?

У меня есть Post модель и Comment модель. Когда пользователь публикует комментарий, postID будет отправлен в качестве параметра.

После сохранения комментария id будет вытолкнут в comments массив который принадлежит посту.

Post модель имеет commentCount который является виртуальным свойством, которое будет подсчитывать, сколько идентификаторов комментариев в comments массив.

Это мой комментарийКонтроллер:

  create(req, res, next) {
    const commentProps = req.body;
    const postId = req.params.id;

    Comment.create(commentProps)
      .then(comment => {
        Post.findById({ _id: postId })
          .then(post => {
            console.log(post);
            console.log('comment id is ' + comment._id);

            post.comments.push(comment._id);

            console.log(post);
            console.log('commentCount is ' + post.commentCount);
          });
        return res.send(comment);
      })
      .catch(next);
  },

Все журналы проходят через так, как они должны быть. comment._id толкают прямо в comments массив и commentCount был увеличен на 1,

Тем не менее, мое тестовое утверждение не проходит:

it('POST to /api/comments/:id creates a new comment and increases posts commentCount', done => {
    const post = new Post({
      author: user._id,
      text: 'This is a post',
      createdAt: 0,
      expiresAt: 0,
      voteCount: 0
    });

    post.save()
      .then(() => {
        Comment.count().then(count => {
          request(app)
            .post(`/api/comments/${post._id}`)
            .send({
              author: user._id,
              text: 'This is a comment',
              createdAt: 0,
              postId: 'randomPost'
            })
            .end(() => {
              Comment.count().then(newCount => {
                console.log('Comment count is ' + post.commentCount);

                assert(count + 1 === newCount);
                assert(post.commentCount === 1);

                done();
              });
            });
        });
      });
  });

Лог в тесте выше возвращается как 0 и если я войду post.comments тогда он просто возвращается как пустой массив.

Есть идеи, что я делаю не так, ребята?

Спасибо!

0 ответов

Другие вопросы по тегам