将promise()函数转移到beforeEach()

时间:2017-05-18 05:17:26

标签: jasmine mocha bdd chai

我是第一次写一些BDD单元测试,我想为我的一个测试套件消除一些重复的代码。以下异步单元测试代码工作正常,但我想以某种方式在beforeEach()块中设置Promise,因为我将编写更多it()测试,并且每个都需要运行{ {1}}致电。感谢

db.find(...)

1 个答案:

答案 0 :(得分:1)

这样的事情可以起作用

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);
    });

});