摩卡测试:为什么我的用户身份无效?

时间:2018-10-06 21:54:32

标签: mongodb mongoose mocha mongoose-schema

因此,我已放置了此测试套件。不幸的是,我没有远见卓识来测试我从数据库中回来的用户是joe,而我也没有,因为我已经在这里进行了保存“ joe”用户的测试:

const assert = require('assert');
const User = require('../src/user');

describe('Creating records', () => {
  it('saves a user', done => {
    const joe = new User({ name: 'Joe' });

    joe.save().then(() => {
      // Has joe been saved successfully
      assert(!joe.isNew);
      done();
    });
  });
});

它正在过去。我继续创建这些测试:

const mongoose = require('mongoose');
const assert = require('assert');
const User = require('../src/user');
const Comment = require('../src/comment');
const BlogPost = require('../src/blogPost');

describe('Association', () => {
  let joe, blogPost, comment;
  beforeEach(done => {
    joe = new User({ name: 'Joe' });
    blogPost = new BlogPost({
      title: 'Eloquent JavaScript',
      content: 'As programs get bigger, they also become more complex'
    });
    comment = new Comment({ content: 'Love this post' });
    joe.blogPosts.push(blogPost);
    blogPost.comments.push(comment);
    comment.user = joe;

    Promise.all([joe.save(), blogPost.save(), comment.save()]).then(() =>
      done()
    );
  });

  it('saves a relation between a user and a blogpost', done => {
    User.findOne({ name: 'Joe' })
      .populate('blogPosts')
      .then(user => {
        assert(user.blogPosts[0].title === 'Eloquent JavaScript');
        done();
      });
  });

他们通过了,但是当我在这里添加此测试时:

xit('saves a full relation graph', () => {
    User.findOne({ name: 'Joe' })
      .populate({
        path: 'blogPosts',
        populate: {
          path: 'comments',
          model: 'comment',
          populate: {
            path: 'user',
            model: 'user'
          }
        }
      })
      .then(user => {
        assert(user.name === 'Joe');
        assert(user.blogPosts[0].title === 'Eloquent JavaScript');
        assert(
          user.blogPosts[0].comments[0].content ===
            'As programs get bigger, they also become more complex'
        );
        assert(user.blogPosts[0].comments[0].user.name === 'Joe');
        done();
      });
  });

失败。我在控制台上记录了日志,发现对于用户我正在返回null,但对于为什么返回空值我还是不清楚。这就是我终端的输出:

{ _id: 5bb92e5fe6b40e97e21cd95f,
  user: null,
  content: 'Love this post',
  __v: 0 }

当我通过控制台记录来检查该测试是否成功时:

it('saves a full relation graph', () => {
    User.findOne({ name: 'Joe' })
      .populate({
        path: 'blogPosts',
        populate: {
          path: 'comments',
          model: 'comment',
          populate: {
            path: 'user',
            model: 'user'
          }
        }
      })
      .then(user => {
        console.log(user.blogPosts[0].comments[0]);
        done();
      });
  });

有人能看到我为什么得到user: null而不是'Joe'吗?

1 个答案:

答案 0 :(得分:0)

您需要在exec()之后利用populate来获得实际的承诺:

xit('saves a full relation graph', () => {
    User.findOne({ name: 'Joe' })
      .populate({
        path: 'blogPosts',
        populate: {
          path: 'comments',
          model: 'comment',
          populate: {
            path: 'user',
            model: 'user'
          }
        }
      }).exec()
      .then(user => {
        assert(user.name === 'Joe');
        assert(user.blogPosts[0].title === 'Eloquent JavaScript');
        assert(
          user.blogPosts[0].comments[0].content ===
            'As programs get bigger, they also become more complex'
        );
        assert(user.blogPosts[0].comments[0].user.name === 'Joe');
        done();
      });
  });