摩卡测试没有返回mongod保存

时间:2017-11-24 16:38:49

标签: node.js mongodb testing mocha integration

我在使用nodejs,MongoDB和mocha实现的用户验证方法测试时遇到了问题。

错误

^CHarrys-MacBook-Pro:Phonex hcbh96$ npm run test-watch

> ls@1.0.0 test-watch /Users/hcbh96/Desktop/PhonexDevelopment/Phonex
> nodemon --exec 'npm test'

[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm test`

> ls@1.0.0 test /Users/hcbh96/Desktop/PhonexDevelopment/Phonex
> mocha tests/**/*.test.js



Node app is running at localhost: + 3000
  Post /tempUser
{ phone: '5005550006',
  countryCode: '+1',
  email: 'test@test.com' }
create temp:{ phone: '5005550006',
  countryCode: '+1',
  email: 'test@test.com',
  _id: 5a18499b182f435b58eed843,
  createdAt: 2017-11-24T16:32:27.089Z,
  isVerified: false }
    1) should create a new tempUser


  0 passing (2s)
  1 failing

  1) Post /tempUser
       should create a new tempUser:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

以下代码是代码的一部分,似乎是导致问题的测试:

tempUser.js - controller

// create a new user based on the request
exports.create = function(request, response) {
    const params = request.body;
    console.log(params);
    // Create a new user based on params
    const tempUser = new TempUser({
        phone: params.phone,
        countryCode: params.countryCode,
        email: params.email
    });
    console.log(`create temp:${tempUser}`);

    // tempUser.save(function(err, doc) {
    //     if (err) {
    //       console.log(doc);
    //         response.send(err);
    //     } else {
    //         // If the user is created successfully, send them an account
    //         // verification token
    //         tempUser.sendAuthyToken(function(err) {
    //             if (err) {
    //                 console.log("err");
    //                 response.send(err);
    //             }
    //         });
    //     response.send(doc);
    //   }
    // });

    tempUser.save().then((doc)=> {
      // If the user is created successfully, send them an account verification token
      // tempUser.sendAuthyToken((err)=>{
      //   response.send(err)
      // });
      console.log(`IMHERE2 ${doc}`);
      response.send(doc);

    }, (err) => {
      console.log(err);
      response.send(err)
    });
};

以下是测试:

使用test-watch通过命令行运行测试,因此nodemon正在运行,我认为这可能是问题的一部分:

配置

"test": "mocha tests/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"

TempUserTest.js

const expect = require('expect');
const request = require('supertest');


//local dependencies
const {app} = require('../../config/initialisers/server.js');
const {User} = require('../../api/models/user/user.js');
const {TempUser} = require('../../api/models/user/tempUser.js')


describe("Post /tempUser", ()=>{

  it('should create a new tempUser',(done)=>{
    var userEmail = "test@test.com";
    var userPhone = "5005550006";
    var userCountryCode = "+1";

    request(app)
    .post('/api/users/temp-user/create')
    .send({
      phone: userPhone,
      countryCode: userCountryCode,
      email : userEmail,
    })
    .expect(200)
    .expect('Content-Type', /json/)

    .expect((res)=>{
****NO RES COMING IN HERE**********************************************
      expect(res.body).toInclude({
        email: userEmail,
        countryCode: userCountryCode,
        phone: userPhone
      });
    })
    .end((err,res) => {
      if(err){
        return done(err);
      }
      TempUser.find().then((tempUsers)=>{
        expect(tempUsers.length).toBe(1);
        expect(tempUsers[0].countryCode).toBe(userCountryCode);
        expect(tempUsers[0].email).toBe(userEmail);
        expect(tempUsers[0].phone).toBe(userPhone);
        done();
      }).catch((e)=>done(e))
    });
  });
});

POSTMAN成功发布

{
    "phone": "5005550006",
    "countryCode": "+1",
    "email":"test@test.com"
}

POSTMAN成功回复

{
    "__v": 0,
    "phone": "5005550006",
    "countryCode": "+1",
    "email": "test@test.com",
    "_id": "5a18485aa191f65b445341c1",
    "createdAt": "2017-11-24T16:27:06.133Z",
    "isVerified": false
}

提前感谢您对此的回复。

我的感觉是,这在测试文件的某个地方是一个愚蠢的小错误,但遗憾的是我没有使用Nodejs来发现它。

0 个答案:

没有答案