【问题标题】:Clean down the index before each text (Mocha, Elastic)在每个文本之前清理索引(Mocha,Elastic)
【发布时间】:2014-08-29 09:08:46
【问题描述】:

我们如何在每次测试前清除索引? (目前它未能通过我的测试,超时)

我试过了

  • DeleteBy(这个词和q我都试过了)
  • http 删除
  • 删除索引

我有以下代码:

var assert = require("assert"),
    elasticsearch = require('elasticsearch'),
    request = require('request'),
    q = require('q'),
    client;

client = new elasticsearch.Client();

describe('people', function(){

    beforeEach(function(done){

        //is this correct?
        //looks like poeple get deleted.
        client.deleteByQuery({
            index: 'people',
            q: '*'
        }, function (error, response) {
            done();
        });

        //I have also tried the following:

        //this way returns an accept code.

//       var deleteOptions = {
//           method: 'DELETE',
//           uri: 'http://localhost:9200/people/person'
//       };
//
//       webApi(deleteOptions).then(function(data){
//           //{"acknowledged":true}
//           console.log(data.body);
//           done();
//       });


        //the following throws an exception

        //delete index

//       client.indices.delete('people').then(function(del){
//           console.log(del);
//           client.indices.create('people').then(function(ct){
//               console.log(ct);
//               done();
//           });
//       });






    });

    describe('add a entry into the index', function(){
        it('should add Dave as a person to the database', function(done){

            //THIS TEST WILL FAIL
            //Error: timeout of 2000ms exceeded

            //I have tried adding a timeout.

            assert.equal(1,1);
        });
    });
});

var webApi = function(options){
    var deferred = q.defer();
    var handle = function (error, response, body) {
        //console.log(body);
        if(error) {
            deferred.reject(error);
        }
        else {
            deferred.resolve({response:response, body:body});
        }
    };
    request(options, handle);
    return deferred.promise; //NOTE: we now return a promise
};

【问题讨论】:

    标签: node.js asynchronous elasticsearch mocha.js


    【解决方案1】:

    您用THIS TEST WILL FAIL 标记的测试因超时而失败,因为您从未在其中调用done()。您已经使用参数声明了传递给it 的匿名函数,该参数告诉Mocha 您的测试是异步的(即使其中的代码现在不是异步的),因此Mocha 等待done() 被调用。您可以调用它,也可以从函数中删除参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 2014-11-24
      • 2021-08-04
      • 2017-06-14
      • 2018-10-29
      相关资源
      最近更新 更多