我有這樣的摩卡測試:摩卡用的NodeJS斷言掛起/超時的斷言(假),而不是錯誤
describe 'sabah', →
beforeEach →
@sabahStrategy = _.filter(@strats, { name: 'sabah2' })[0]
.strat
it 'article list should be populated', (done) →
@timeout 10000
strat = new @sabahStrategy()
articles = strat.getArticleStream('barlas')
articles.take(2).toArray((result)→
_.each(result, (articleList) →
// I make the assertions here
// assert(false)
assert(articleList.length > 1)
)
done()
)
的問題是,每當我做assert(false)
,測試掛起,直到超時,而不是給出斷言錯誤,爲什麼?
編輯:
例如,如果我有這兩個測試
it 'assert false', (done) →
assert(false)
done()
it 'article link stream should be populated', (done) →
@timeout 20000
articles = @sabahStrategy.articleLinkStream('barlas')
articles.pull((err, result)→
console.log('here')
assert(false)
console.log('after')
assert(!err)
assert(result.length > 1);
_.each(result, (articleList) →
assert(articleList.link)
)
done()
)
第一個,給預期的斷言錯誤,第二個,日誌here
,並在assert(false)
所以掛after
從不記錄。它是與articles
是一個流和斷言是pull
回調中,這是一個從highland.js API。
解決編輯:
所以根據保羅我固定的代碼的問題:我已經產生的問題的一個簡化版本
:
it 'article stream should be populated', (done) →
@timeout 30000
articles = @sabahStrategy.articleStream('barlas')
articles.pull((err, result) →
try
# assert false properly throws now.
assert(false)
assert(!err)
assert(result.length == 1)
assert(result[0].body)
assert(result[0].title || result[0].title2)
done()
catch e
done(e)
)
EDIT2 :
h = require('highland')
Q = require('q')
describe 'testasynchigh', →
beforeEach →
@deferred = Q.defer()
setTimeout((→
@deferred.resolve(1)
).bind(this), 50)
it 'should throw', (done) →
s = h(@deferred.promise);
s.pull((err, result) →
console.log result
assert false
done()
)
我看到你的版本確實在@Louis工作,但是如果你涉及承諾,摩卡不能處理這個問題,所以它會在這個例子中掛起。也可以嘗試註釋掉assert false
,看看它通過了。
所以路易斯我希望我能引起你的注意,你能否解釋這個問題,try catch
的確看起來很醜,我希望你找到合理的解決方案。
我寫摩卡代碼兩年多也和* *一般來說,您不需要像在回答中顯示的那樣捕獲異常。**很可能''highland.js'正在吞噬異常,或者它在處理回調中的異常方面有一些特殊性。但一般來說,Mocha非常有能力檢測異常,無論是同步還是異步。只要運行'it(「test」,function(done){setTimeout(function(){throw new Error();},100);});'('done'不被調用,這不是錯誤。摩卡將檢測到沒有任何問題的例外。 – Louis 2014-11-07 23:17:05
@Louis,你的意思是吞嚥像這裏描述https://github.com/caolan/highland/issues/121,你認爲這可能是問題? – user3995789 2014-11-08 01:22:51
@ user3995789是的,我認爲這是問題所在。我已經嘗試過使用RSVP(另一個承諾庫)的最新代碼片段,並獲得與Q相同的結果。我也嘗試使用Highland的'.errors('call但我沒有得到任何東西。 – Louis 2014-11-08 12:00:48