Jasmine 将承诺从it()函数移动到beforeach()函数

Jasmine 将承诺从it()函数移动到beforeach()函数,jasmine,mocha.js,bdd,chai,Jasmine,Mocha.js,Bdd,Chai,我第一次编写了一些BDD单元测试,我想为我的一个测试套件消除一些重复的代码。下面的异步单元测试代码运行良好,但我想在beforeach()块中以某种方式设置承诺,因为我将编写更多的it()测试,并且每个测试都需要运行db.find(…)调用。谢谢 describe('DB retrieve row', function() { beforeEach(function () { // i'd like to set up the promise in this block

我第一次编写了一些BDD单元测试,我想为我的一个测试套件消除一些重复的代码。下面的异步单元测试代码运行良好,但我想在beforeach()块中以某种方式设置承诺,因为我将编写更多的it()测试,并且每个测试都需要运行
db.find(…)
调用。谢谢

describe('DB retrieve row', function() {
    beforeEach(function () {
        // i'd like to set up the promise in this block
    });

    it("returns a least one result", function () {
        function success(orderData) {
            // keep the following line in this it() block
            expect(orderData.length).to.be.ok;
        }

        function fail(error) {
            new Error(error);
        }

        return db.find('P9GV8CIL').then(success).catch(fail);
    });

});

像这样简单的事情就行了

describe('DB retrieve row', function() {

    var promise;

    beforeEach(function () {
        promise = db.find('P9GV8CIL')
    });

    it("returns a least one result", function () {
        function success(orderData) {
            // keep the following line in this it() block
            expect(orderData.length).to.be.ok;
        }

        function fail(error) {
            new Error(error);
        }

        return promise.then(success).catch(fail);
    });

});