2017-10-20 91 views
1

我試圖運行一個承諾的測試,但測試失敗爭​​論超過超時限制,並建議確保我有完成子句。摩卡,nodejs承諾測試不能完成,因爲缺乏完成

這是我的測試代碼的一部分:

$configurations 
    .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model 
    .then(function() { 
     done(new Error("Expected INVALID_MODEL error but got OK")); 
    }, function (error) { 
     chai.assert.isNotNull(error); 
     chai.expect(error.message).to.be.eq("INVALID_MODEL_ERROR"); 
     chai.expect(error.kind).to.be.eq("ERROR_KIND"); 
     chai.expect(error.path).to.be.eq("ERROR_PATH"); 
     done(); 
    }) 
    .catch(done); 
}); 

我都在那裏完成的條款,你可以看到,所以我不知道如果我錯過測試或結構的東西是錯的。

回答

4

摩卡支持測試諾言,但不使用done只要您的諾言爲return

const expect = chai.expect 

it('should error', function(){ 
    return $configurations 
    .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model 
    .then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")}) 
    .catch(error => { 
     expect(error).to.not.be.null; 
     expect(error.message).to.equal("INVALID_MODEL_ERROR"); 
     expect(error.kind).to.equal("ERROR_KIND"); 
     expect(error.path).to.equal("ERROR_PATH"); 
    }) 
}) 

也期待在chai-as-promised讓更多喜歡薛寶釵標準斷言/預期承諾的測試。

chai.should() 
chai.use(require('chai-as-promised')) 

it('should error', function(){ 
    return $configurations 
    .updateConfiguration(configurations_driver.NOT_VALID_MODEL) 
    .should.be.rejectedWith(/INVALID_MODEL_ERROR/) 
}) 

在節點7.6+環境或您babel/babel-register你也可以利用async/await承諾處理器的

it('should error', async function(){ 
    try { 
    await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL) 
    throw new Error("Expected INVALID_MODEL error but got OK")}) 
    } catch (error) { 
    expect(error).to.not.be.null; 
    expect(error.message).to.equal("INVALID_MODEL_ERROR"); 
    expect(error.kind).to.equal("ERROR_KIND"); 
    expect(error.path).to.equal("ERROR_PATH"); 
    } 
})