2017-05-18 20 views
0

我正在编写一些BDD单元测试,我想为我的一个测试套件省去一些重复代码。下面的异步单元测试代码工作正常,但我想以某种方式在beforeEach()块中设置Promise,因为我将编写更多的it()测试,并且每个测试都需要运行db.find(...)调用。由于将承诺从它()函数移动到beforeEach()

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

}); 

回答

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

});