Тайм-ауты Mocha во время подключения MongoDB - PullRequest
0 голосов
/ 12 мая 2018

Монго Новичок здесь.Я пытаюсь подключить модульное тестирование моих коллекций mongo db, используя Mocha, Chai.Но каждый раз, когда я запускаю тест, кажется, что соединение истекает.Я увеличил timeout в mocha.opts до 50K мс, но, похоже, время соединения истекло.Я не могу понять причину, почему?

Вот мой код

use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const chai = require('chai');
const expect = chai.expect;



/**
 * 
 * Create a new schema that accepts a firstname and lastname and employee id
 */

 const testSchema = new Schema({
     firstname : {type: String,required:true},
     lastname : {type: String, required:true},
     id : {type : Number, required : true}
 });

 /**
  * 
  * Create a new collection name employee_details 
  */

  const employee_details = mongoose.model('employee_details',testSchema);

  describe('Create a connection with the database',()=>{
      before((done)=>{
          mongoose.connect('mongodb://127.0.0.1:27017/new_demo');
   //I tried changing the url to mongodb://localhost/new_demo but it didn't work
          const db  = mongoose.connection;
          db.on('error',console.error.bind(console,'Error connecting to DB'));
          db.once('open',()=>{
              console.log('Connected to new_demo db');
              done();
          });
      });

  });


  describe('Test Database function',()=>{
      //Save something with value Mike Stevens, 19981
      it('saves a new record',(done)=>{
        var first_record = employee_details({
            firstname : 'Mike',
            lastname : 'Stevens',
            id : 19981
        });

        first_record.save(done);
      });



      after((done)=>{
          mongoose.connection.db.dropDatabase(()=>{
              mongoose.connection.close(done);
          });
      });
  });

То, что я пробовал до сих пор

  • Увеличено время ожидания мокко
  • Изменен URL-адрес соединения на mongodb://localhost/new_demo и mongodb://localhost:27017/new_demo
  • Запустил mongod в оболочке и затем попытался запустить тест mocha

Сообщение об ошибке

Error: Timeout of 50000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/ab/ab/abcd/api-testing/mongo-testing/my-mongo-tests/test/create_connection.js)

Согласно сообщению об ошибке, должен быть вызван done, что делается в коде.Тогда почему эта ошибка?

Редактировать 1: полное сообщение об ошибке

Test Database function
    1) saves a new record
    2) "after all" hook


  0 passing (50s)
  2 failing

  1) Test Database function
       saves a new record:
     Error: Timeout of 50000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/ab/ab/abcd/api-testing/mongo-testing/my-mongo-tests/test/create_connection.js)


  2) Test Database function
       "after all" hook:
     TypeError: Cannot read property 'dropDatabase' of undefined
      at Context.after (test/create_connection.js:56:34)

1 Ответ

0 голосов
/ 12 мая 2018

Тестовый пример mocha ничем не отличается от обычного кода JS, поэтому он будет следовать за выделением области действия и потоком кода JS.Я попытался исправить контрольный пример, чтобы разрешить переменный доступ в различных тестовых случаях и до / после ловушек.

Обратите внимание, что я не выполнил реальный тестовый пример, и вам может потребоваться изменить приведенный ниже код, чтобы он успешно выполнялся для вас

'use strict';
 const mongoose = require('mongoose');
 const Schema = mongoose.Schema;
 const chai = require('chai');
 const expect = chai.expect;



 /**
  * 
  * Create a new schema that accepts a firstname and lastname and employee id
  */
 const testSchema = new Schema({
     firstname: { type: String, required: true },
     lastname: { type: String, required: true },
     id: { type: Number, required: true }
 });

 /**
  * 
  * Create a new collection name employee_details 
  */
 const employee_details = mongoose.model('employee_details', testSchema);

 /**
  * Decalre the db const in this(global) context so this variable is visible by all test cases
  */
 const db;

 //  describe('Create a connection with the database', () => {
 //      // Seems redundet to something
 //      // Also the context is lost if you do this .. 
 //      // Thus not allowing for you to use variables .. const db in this case
 //      // If you still want this describe, you will have to use it like any other javascript function ... to expose the variables
 //  });


 describe('Test Database function', () => {

     // connect to database     
     before((done) => {
         mongoose.connect('mongodb://127.0.0.1:27017/new_demo');
         //Keep the url same which you use to debug you local application
         db = mongoose.connection;
         db.on('error', console.error.bind(console, 'Error connecting to DB'));
         db.once('open', () => {
             console.log('Connected to new_demo db');
             done();
         });
     });

     //Save something with value Mike Stevens, 19981
     it('saves a new record', (done) => {
         // Also if you want to increase the deafult timeout of a teast case
         // you will have to change the => to function(), because of the way 'this' context behaves
         // Thus : 
         //   it('saves a new record', function(done) {
         //      this.timeout(10000);
         //      .. test case code
         //   }
         var first_record = employee_details({
             firstname: 'Mike',
             lastname: 'Stevens',
             id: 19981
         });

         first_record.save(function(err) {
             if (err) return handleError(err);
             // saved!
             done();
             //I used a simple callback function instead, makes life easier and code understable
         })
     });



     after((done) => {
         mongoose.connection.db.dropDatabase(() => {
             mongoose.connection.close(done);
             // I am guessing same goes here .. 
             // keep it simple !!
         });
     });
 });

Теперь немного теории, в идеале это , а не , рекомендуется, а точнее, не входит в сферу случая модульного теста для фактического подключения к базе данных и / или изменения состояния * 1014.* внешнего объекта каким-либо образом (поскольку вы специально упомянули, что это «блок-тест»).

В модульном тесте следует избегать внешних вызовов или вызова реальных API.Мы должны заглушить вызов и в нашем тестовом примере заявить, что вызов был сделан, когда мы ожидали, что это было, или когда был предоставлен соответствующий ввод.

Вот пример, чтобы пройти разговор:

//This is myGLobalServiceLoactor which is used in actual code which is to be tested.
 myGLobalServiceLoactor = {
     database: {
         save: sinon.stub(),
         find: sinon.stub()
     }
 }

 it('to check if external method is called ', () => {
     let person_to_Save = {
         //. . . 
         //. . . 
     }
     proxyPersonInterface.savePerson(input_person).then((status) => {
         // check if our stubbeb function is called
         assert(myGLobalServiceLoactor.database.save.calledOnce);
     });

 });

И вы можете использовать вспомогательные библиотеки, такие как Sinon и rewire , чтобы заглушки и прокси фактических модулей в модульном тесте.Надеюсь, это поможет.

...