2013-09-27 56 views
3

我停留在我的測試代碼與摩卡,其中測試包括在getUserMedia回調的一部分:摩卡:有()完成和回調錯誤

it("should work without error", function() { 
    navigator.getUserMedia({fake:true}, function(stream) { 
     expect(3).to.equal(3); 
     done(); // done is not defined if expect() is valid 
    },console.error); 
}); 

這裏,做()不是定義,但測試是成功的。

it("should NOT work", function() { 
    navigator.getUserMedia({fake:true},function(stream) { 
     expect(3).to.equal(4); 
     done(); 
    },console.error); 
}); 

在這裏,我得到一個錯誤:

AssertionError: expected 3 to equal 4 

,但摩卡接口仍顯示爲測試驗證。 (綠色勾號)

我做錯了什麼,或完成()是否被竊聽?

回答

3

你的函數應該得到一個完成的參數。

it("should get done", function(done) { 
    expect(3).to.equal(3); 
    expect(3).not.to.equal(4); 
}); 

但是你應該用做只有當你有異步功能在測試 如果不是測試應該是這樣的:

it("should not be async", function() { 
    expect(3).to.equal(3); 
}