【问题标题】:Moving a promise from it() function to beforeEach()将 promise 从 it() 函数移到 beforeEach()
【发布时间】:2017-05-18 05:17:26
【问题描述】:

我是第一次编写一些 BDD 单元测试,我想为我的一个测试套件消除一些重复的代码。以下异步单元测试代码工作正常,但我想以某种方式在 beforeEach() 块中设置 Promise,因为我将编写更多 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);
    });

});

【问题讨论】:

    标签: jasmine mocha.js bdd chai


    【解决方案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);
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 2021-12-22
      • 2022-12-09
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2019-07-18
      相关资源
      最近更新 更多