好的,我们遇到了完全相同的问题,并设计了一个非常优雅的解决方案,它不需要超时,并且是运行测试的最快方法。基本上,我们使用两种策略之一,具体取决于您正在等待的屏幕元素。
所有代码都进入tests/mocha/client/lib.coffee,不是100% Jasmine 等效代码,但它应该可供所有客户端测试代码使用。我把它留在了 Coffeescript 中,但是你可以在 Coffeescript.org 上将它编译成 Javascript,它也应该可以正常工作。
如果您所做的任何事情(路由或其他诸如更改反应变量之类的事情)都会导致Template
要(重新)渲染,您可以使用Template.<your_template>.rendered
钩子来检测渲染何时完成。因此,我们在 lib.coffee 中添加了以下函数:
@afterRendered = (template,f)->
cb = template.rendered
template.rendered = ->
cb?()
template.rendered = cb
f?()
return
return
它有什么作用?它基本上“记住”了原作rendered
回调和暂时地将其替换为调用额外函数的函数after the template
被渲染并调用原始回调。它需要做这种内务处理以避免破坏任何可能依赖于rendered
回调,因为你基本上是直接搞乱 Meteor 代码。
在您的测试中,您可以执行以下操作:
it.only "should check stuff after routing", (done)->
try
Router.go "<somewhere>"
afterRendered Template.<expected_template>, ->
<your tests here>
done()
catch e
done(e)
我也推荐 try-catch,因为我注意到异步错误并不总是进入速度系统,只会给你一个超时失败。
好吧,还有一些东西实际上并不重新渲染,而是用 JS 或某种“显示/隐藏”机制生成的。为此,您确实需要某种超时,但是您可以通过使用轮询机制来减少超时的“时间成本”。
# evaluates if a JQuery element is visible or not
$.fn.visible = -> this.length > 0 and this.css('display') isnt 'none'
# This superduper JQuery helper function will trigger a function when an element becomes visible (display != none). If the element is already visible, it triggers immediately.
$.fn.onVisible = (fn,it)->
sel = this.selector
if this.visible()
console.log "Found immediately"
fn?(this)
else
counter = 0
timer = setInterval ->
counter++
el = $(sel)
if el.visible()
fn?(el)
clearInterval timer
console.log "Found on iteration #{counter}"
else
it?(el)
, 50
您可以删除控制台日志记录和辅助日志记录it
如果您愿意,可以使用迭代器函数,它们并不重要。这允许您在测试中执行类似的操作:
$('#modalId').onVisible (el)->
<tests here>
done()
, (el)->
console.log "Waiting for #{el.selector}"
如果需要,您可以删除第二个功能,它是it
上面提到的迭代器函数。但是,请注意,此特定代码使用“display:hidden”作为不可见性标记(Bootstrap 就是这样做的)。如果您的代码使用其他机制来隐藏/显示部件,请更改它。
对我们来说就像一个魅力!