Подтвердить что ты не робот

Тестирование Promise-цепочек с мокко

У меня есть следующий стиль функции (с использованием bluebird promises под Node.JS):

module.exports = {
    somefunc: Promise.method(function somefunc(v) {
        if (v.data === undefined)
            throw new Error("Expecting data");

        v.process_data = "xxx";

        return module.exports.someother1(v)
          .then(module.exports.someother2)
          .then(module.exports.someother3)
          .then(module.exports.someother4)
          .then(module.exports.someother5)
          .then(module.exports.someother6);
    }),
});

который я пытаюсь проверить (используя mocha, sinon, assert):

// our test subject
something = require('../lib/something');

describe('lib: something', function() {
    describe('somefunc', function() {
        it("should return a Error when called without data", function(done) {
            goterror = false;
            otherexception = false;
            something.somefunc({})
            .catch(function(expectedexception) {
                try {
                    assert.equal(expectedexception.message, 'Expecting data');
                } catch (unexpectedexception) {
                    otherexception = unexpectedexception;
                }
                goterror = true;
            })
            .finally(function(){
                if (otherexception)
                    throw otherexception;

                 assert(goterror);
                 done();
            });
        });
});

Все, что работает как таковое, но чувствует себя запутанным для одного.

Моя основная проблема - тестирование функции Promises -chain (и порядка) в функции. Я пробовал несколько вещей (подделка объекта с помощью метода, который не работал, издеваясь над ним как сумасшедший и т.д.); но, похоже, что-то я не вижу, и я, похоже, не получаю мокко-синоническую документацию в этом отношении.

У кого-нибудь есть указатели?

Спасибо

Количество

4b9b3361

Ответ 1

Mocha поддерживает promises, чтобы вы могли сделать это

describe('lib: something', function() {
    describe('somefunc', function() {
        it("should return a Error when called without data", function() {
            return something.somefunc({})
                .then(assert.fail)
                .catch(function(e) {
                    assert.equal(e.message, "Expecting data");
                });
        });
    });
});

Это примерно эквивалентно синхронному коду:

try {
   something.somefunc({});
   assert.fail();
} catch (e) {
   assert.equal(e.message, "Expecting data");
}