簡體   English   中英

摩卡測試無法通過Express REST API

[英]mocha test fails express REST API

以下是我的Accounts.js模塊。

var Accounts  = function(){
    return{
        registerNewUser : function(req,res){
            console.log(req.body);
            var email = req.body.email;
            var password = req.body.password;

            if(email === undefined || password === undefined){
                res.status(400).end('Required Fields Missing');
            }
            else{
                email = email.trim();
                password = password.trim();   
                console.log('lengths = ', email.length, ' password length = ', password.length);
                if(email.length <= 0 || password.length <= 0){
                    res.status(400).end('Bad Request');
                }
                else{
                    res.status(200).end('ok');
                }
            }


        }
    }

}

module.exports = new Accounts();

摩卡UnitTests.js

var request = require('supertest');
var app = require('../express-app');
describe('Testing Route to Register New User', function(){
    var server;
    beforeEach(function(){
        server = app.startServer();
    });
    afterEach(function(done){
       server.close(); 
       done();
    });
   it('Missing required fields test', function(done){
       request(app).post('/register',{           
       }).expect(400).end(done);

   }) ;
   it('Empty field test',function(done){
      request(app).post('/register',{
          email:"",
          password:"           "
      }).expect(400).end(done);       
   });

    it('Should accept the correct email and password',function(done){
      request(app).post('/register',{
          email:"email",
          password:"alskdjfs"
      }).expect(200).end(done);       
   });

});

摩卡輸出:

  Testing Route to Register New User
API Server listening on port 3000
{}
    ✓ Missing required fields test
API Server listening on port 3000
{}
    ✓ Empty field test
API Server listening on port 3000
{}
    1) Should accept the correct email and password


  2 passing (65ms)
  1 failing

  1) Testing Route to Register New User Should accept the correct email and password:
     Error: expected 200 "OK", got 400 "Bad Request"
      at Test._assertStatus (node_modules/supertest/lib/test.js:232:12)
      at Test._assertFunction (node_modules/supertest/lib/test.js:247:11)
      at Test.assert (node_modules/supertest/lib/test.js:148:18)
      at Server.assert (node_modules/supertest/lib/test.js:127:12)
      at emitCloseNT (net.js:1521:8)

我已經使用curl cli測試了上述路由,它可以按預期方式工作,類似地,也可以按瀏覽器表單中的預期方式工作,我不知道為什么摩卡失敗了? 有人可以幫我解決這個問題嗎?

問候

您可能以錯誤的方式發送數據。 嘗試這個

it('Should accept the correct email and password',function(done){
      var data = {
          email:"email",
          password:"alskdjfs"
      };
      request(app).post('/register').send(data).expect(200).end(done);       
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM