2016-07-25 3 views
0

Почему проходит следующее испытание?500 равно 400 проходам в тесте мокко?

"use strict"; 

const 
    path = require('path'), 
    Dexter = require('../src/Dexter.js'), 
    chai = require('chai'), 
    chaiHttp = require('chai-http'), 
    expect = chai.expect, 
    dexterServer = new Dexter(path.resolve(__dirname, 'test/data/sample.har')); 

chai.use(chaiHttp); 
describe('Rest API',() => { 
    before(() => { 
     dexterServer.startUp(); 
    }); 

    it('should\'ve started the server', function() { 
     chai.request('http://127.0.0.1:1121') 
      .get('/') 
      .end(function(err, response){ 
       console.log(response.status); 
       expect(500).to.equal(400);// This passes? What? 
       done(); 
      }); 
    }); 

    after(() => { 
     dexterServer.tearDown(); 
    }); 
}); 

Когда я делаю console.log из response.status, я вижу 200. Но когда я

expect(response.status).to.equal(400);//response.status is an int 

проходит тест!

Что я делаю неправильно?

ответ

1

Вы забыли пройти обратный вызов. it рассматривался как синхронизация с 0 предположениями.

it('should\'ve started the server', function (done) { 
    chai.request('http://127.0.0.1:1121') 
     .get('/') 
     .end(function(err, response){ 
      console.log(response.status); 
      expect(500).to.equal(400);// This passes? What? 
      done(); 
     }); 
}); 
+0

Спасибо за указание, что мне пришлось пройти в 'done'! Это закрепило мой тест! –

0

Вы должны пройти done в it, before и after заявление поддерживать асинхронный поток.

describe('Rest API', (done) => { 
before(() => { 
    dexterServer.startUp(); 
}); 

it('should\'ve started the server', function (done) { 
    chai.request('http://127.0.0.1:1121') 
     .get('/') 
     .end(function(err, response){ 
      console.log(response.status); 
      expect(500).to.equal(400);// This passes? What? 
      done(); 
     }); 
}); 

after((done) => { 
    dexterServer.tearDown(); 
}); 
}); 
0

Мокко поддерживает обещания, так что вы можете использовать тот факт, что chai-http производит обещания и просто вернуть обещание:

it('should\'ve started the server', function() { 
    // Return the promise. 
    return chai.request('http://127.0.0.1:1121') 
     .get('/') 
     // Use .then instead of .end. 
     .then(function(response){ 
      console.log(response.status); 
      expect(500).to.equal(400); 
     }); 
}); 

Если вам нужно сделать специальную обработку ошибок, вы могли бы иметь .catch тоже. В противном случае вы можете позволить Mocha обрабатывать любую ошибку как сбой.

Смежные вопросы