gpt4 book ai didi

javascript - 使用 Velocity 和 Jasmine 测试 Meteor 时需要超时

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:55 24 4
gpt4 key购买 nike

对 meteor、velocity 和 jasmine 还很陌生,所以不确定我是否做错了什么,将 Jasmine 用于它不是为它设计的目的,或者这就是它的工作方式。

我发现我需要为几乎所有测试设置超时才能让它们通过。应该是这种情况还是我做错了什么?

例如我正在运行的一些测试来检查验证消息:

    describe("add quote validation", function() {
beforeEach(function (done) {
Router.go('addQuote');
Tracker.afterFlush(function(){
done();
});
});

beforeEach(waitForRouter);

it("should show validation when Quote is missing", function(done) {
$('#quote').val('');
$('#author').val('Some author');
Meteor.setTimeout(function(){
$('#addQuoteBtn').click();
}, 500);
Meteor.setTimeout(function(){
expect($('.parsley-custom-error-message').text()).toEqual("Quote can't be empty.");
done();
}, 500);
});
}

最佳答案

好吧,我们遇到了这个完全相同的问题并设计了一个非常优雅的解决方案,它不需要超时并且是运行测试的最快方式。基本上,我们使用两种策略之一,具体取决于您等待的屏幕元素。

所有代码都进入 tests/mocha/client/lib.coffee,不是 100% 的 Jasmine 等价物,但它应该对所有客户端测试代码可用。我把它留在了 Coffeescript 中,但你可以在 coffeescript.org 上将它编译成 Javascript,它应该也能正常工作。

如果您所做的任何事情(路由或其他类似更改 react 变量)导致 Template要(重新)渲染,您可以使用 Template.<your_template>.rendered钩子(Hook)来检测何时完成渲染。因此,我们在 lib.coffee 中添加了以下函数:

@afterRendered = (template,f)->
cb = template.rendered
template.rendered = ->
cb?()
template.rendered = cb
f?()
return
return

它有什么作用?它基本上“记住”了原来的 rendered回调,暂时将其替换为调用额外函数之后 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上面提到的迭代器函数。但是,请注意,此特定代码使用“显示:隐藏”作为隐形标记(Bootstrap 执行此操作)。如果您的代码使用另一种机制来隐藏/显示部件,请更改它。

对我们来说就像一个魅力!

关于javascript - 使用 Velocity 和 Jasmine 测试 Meteor 时需要超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28335147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com