错误:超时超过9999ms。对于异步测试和挂钩,请确保调用了“ done()”;如果返回一个承诺,

时间:2019-12-04 15:59:01

标签: javascript node.js mongodb mongoose mocha

我有一个针对chai的基本测试,我正在学习node.js api

测试如下:


const BlogPost = require("../app/models/blogpost.model");
const server = require("../index");

const chai = require("chai");
const expect = chai.expect;
const chaiHttp = require("chai-http");
chai.use(chaiHttp);

const blogURL = "/api/blogPosts";

// blogpost.test.js
describe('Blog Posts', () => {
    beforeEach(done => {
      BlogPost.remove({}, err => {
        if(err) console.error(err);
        done();
      });
    });
    describe('/POST/ - publish a BlogPost', () => {
      it('it should POST a new BlogPost', done => {
        const NewBlogPost = {
          url: 'my-first-blog-post',
          title: 'My First Blog Post',
          body: 'This is some text. Lorem ipsum... etc...',
          tags: ['blog', 'nodejs', 'api']
        };
        chai.request(server).post(blogURL).send(NewBlogPost).end((err, res) => {
              expect(res).to.have.status(200);
              expect(res.body).to.be.an('object');
              expect(res.body).to.have.property('msg')
                .eql('Successfully published blog post.');
              expect(res.body).to.have.property('blogPost');
              expect(res.body.blogPost).to.have.property('url').and.be.an('string').eql(NewBlogPost.url);
              expect(res.body.blogPost).to.have.property('title').and.be.an('string').eql(NewBlogPost.title);
              expect(res.body.blogPost).to.have.property('body').and.be.an('string').eql(NewBlogPost.body);
              expect(res.body.blogPost).to.have.property('tags').and.be.an('array').and.have.length(NewBlogPost.tags.length).eql(NewBlogPost.tags);
              expect(res.body.blogPost).to.have.property('date');
              expect(res.body.blogPost).to.have.property('_id');  
              done();      
            });
      });
    });
  });

我正在运行一个mongodb实例,当我运行服务器时,所有服务器都已连接并正在侦听正确的端口等。

我的package.json是

 "scripts": {
    "test": "mocha --timeout 9999 --exit"
  },

那么我们有以下内容

blogpost.controller.js

"use strict"
const BlogPost = require('../models/blogpost.model');

//Create a new blog post 
exports.publishPost = (req, res) => {
    const NewBlogPost = new BlogPost(req.body);
    NewBlogPost.save((err, blogPost) => {
        if(err) {
            return res.status(422).json({
                msg: 'Server encounted an error publishing the blog post',
                error: err
            });
        }
        else {
            return res.status(200).json({
                msg: 'Successfully published the blog post',
                blogpost: blogPost
            })
        }
    })
};

blogpost.modal.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BlogPostSchema = Schema({
    url: {
        type: String,
        required: true,
        unique: true
    },
    title: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    },
    image: {
        type: String,
        required: false
    },
    tags: [String],
    updated: {
        type: Date
    }
});

module.exports = mongoose.model('BlogPost', BlogPostSchema);

我希望有足够的背景信息来寻求帮助。

0 个答案:

没有答案