AssertionError:目标不能为null或未定义

时间:2018-07-17 01:45:35

标签: javascript node.js mocha

我正在使用这个使我感到沮丧的测试套件,因为我继续遇到此错误:

1) BlogPost API resource
       GET endpoint
         should return all existing posts:
     AssertionError: Target cannot be null or undefined.
      at D:\Projects\Thinkful\mongooseBlog02\blog-app-mongoose-challenge-solution\test\test-blog-integration.js:128:54
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

这是指我的test-blog-integration.js文件中的第128行:

describe('GET endpoint', function() {

    it('should return all existing posts', function() {
      // strategy:
      //    1. get back all restaurants returned by by GET request to `/restaurants`
      //    2. prove res has right status, data type
      //    3. prove the number of restaurants we got back is equal to number
      //       in db.
      //
      // need to have access to mutate and access `res` across
      // `.then()` calls below, so declare it here so can modify in place
      let res;
      return chai.request(app)
        .get('/posts')
        .then(function(_res) {
          // so subsequent .then blocks can access response object
          res = _res;
          expect(res).to.have.status(200);
          console.log("testKC");
          // otherwise our db seeding didn't work
          expect(res.body.posts).to.have.lengthOf.at.least(1);
          return allPosts.count();
        })
        .then(function(count) {
          expect(res.body.posts).to.have.lengthOf(count);
        });
    });

我尝试了postsallPosts,但是我仍然遇到相同的错误。我从allPosts文件中获得了models.js

'use strict';

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const blogPostSchema = mongoose.Schema({
  author: {
    firstName: String,
    lastName: String
  },
  title: {type: String, required: true},
  content: {type: String},
  created: {type: Date, default: Date.now()}
});


blogPostSchema.virtual('authorName').get(function() {
  return `${this.author.firstName} ${this.author.lastName}`.trim();
});

blogPostSchema.methods.serialize = function() {
  return {
    id: this._id,
    author: this.author,
    content: this.content,
    title: this.title,
    created: this.created
  };
};

const BlogPost = mongoose.model('allPosts', blogPostSchema);

module.exports = { BlogPost };

1 个答案:

答案 0 :(得分:1)

它出现在您的Expect语句之一中,您期望长度为XXX或类似长度的对象未定义或为null,并且没有要比较的length属性。因此,期望它具有任意值的长度的主张失败了。