2014-09-25 54 views
0

我有困難理解下面的代碼,利用摩卡和薛寶釵在的NodeJS現場測試:功能現場測試採用摩卡和灣仔的NodeJS

suite('Global Tests', function(){ 
     test('page has a valid title', function(){ 
       assert(document.title && document.title.match(/\S/) && 
         document.title.toUpperCase() === 'AHC'); 
     }); 
}); 

什麼是混淆是如何代碼工作還沒有似乎遵循chai documentation斷言。其中格式如下:

斷言(表達式,消息)

回答

1

這是不使所述第二參數(消息)。這不是無效的,只是使得確定測試失敗的原因變得更加困難。如果你想讓測試更清晰,你可以將表達式分解爲三個斷言,並傳遞一條關於如果驗證是否斷言的消息。

suite('Global Tests', function(){ 
    test('page has a valid title', function(){ 
    assert(document.title, 'document title is not defined'); 
    assert(document.title.match(/\S/), 'document title does not have a single character'); 
    assert(document.title.toUpperCase() === 'AHC', 'Document title does not equal AHC'); 
    }); 
});  
+0

我已添加assert('foo'!=='bar','foo is not bar');測試消息的工作方式。我把它放在上面的代碼的不同部分,不能看到消息「foo不是酒吧」。你能用上面的代碼提供一個多重斷言的例子嗎? – JnL 2014-09-25 04:21:06

+0

以示例更新了答案。如果你想看到這個消息,我建議在第三個斷言中改變'AHC'的值。請注意,斷言示例'foo'!=='bar'始終爲真,因此斷言永遠不會發生。 – 2014-09-25 04:31:06

+0

我運行了你的代碼,我發現瀏覽器'頁面中顯示的消息有一個有效的標題'(帶有一個檢查或X取決於是否通過),而不是與assert語句相關的消息。斷言消息是否應該顯示在瀏覽器中? – JnL 2014-09-25 04:38:30