测试失败,那么成功 [英] Test fails then succeeds

查看:98
本文介绍了测试失败,那么成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击运行了好几次 - 通之间的这些测试交替和失败

Click run a couple of times - these tests alternate between pass and fail.

http://jsfiddle.net/samselikoff/hhk6u/3/

这两项测试要求公司,但我不知道怎么的事件隔离开来。任何想法?

Both tests require companies, but I don't know how to isolate the events. Any ideas?

答:

Jeferson是正确的。一个简单的方法来解决这个问题,就是用 events.once 而不是 events.on 。这样,你从每个测试清理活动。

Jeferson is correct. One easy way to solve this, is to use events.once instead of events.on. This way you clean up your events from each test.

推荐答案

您正在运行同步测试,而引发事件的回调是异步的。

You are running synchronous tests while the callbacks of the triggered events are asynchronous.

要解决这个问题,你必须实行asyncTest并调用启动功能测试时断言可供收集。

To fix that you have to implement an "asyncTest" and call the start function when the test assertions are ready to be collected.

您第二次测试的失败消息:

Your second test was failing with the message:

调用start(),而已经开始(QUnit.config.semaphore为0
  已经)

Called start() while already started (QUnit.config.semaphore was 0 already)

睾丸
正是因为它是一个同步测试,已经开始和你再次调用start()方法。

teste Exactly because it was a synchronous test, already started and you were calling the start() method again.

和也是在你的第一次测试,没有指定一个回调函数,你要包装你的异步调用另一个函数,所以你可以调用start()时,模拟AJAX调用已准备就绪。

And also in your first test, that doesn't specify a callback function, you have to wrap your async call in another function so you can call start() when the simulated AJAX call is ready.

我一起工作code更新您的jsfiddle: http://jsfiddle.net/hhk6u/8/
新的code是:

I updated your JSFiddle with working code: http://jsfiddle.net/hhk6u/8/ The new code is:

QUnit.config.autostart = false;
QUnit.config.testTimeOut = 1000;

asyncTest('Some test that needs companies.', function() {
    function getCompanies() {
        var companies = new Companies();
        ok(1);
        start();
    }
    setTimeout(getCompanies, 500);
});

asyncTest('Some other async test that triggers a listener in companies.', function() {   
    var companies = new Companies();

    events.trigger("userSet:company", { name: "Acme", id: 1 });

    stop();
    events.on('fetched:departments', function(response) {
        console.log(response);
        deepEqual(response, [1, 2, 3]);
        start();
    });
});

请注意,在第一测试方法我创建,将一个时间间隔(500毫秒)之后被称为getCompanies功能,应该够AJAX调用来完成

Note that in the first test method I created a "getCompanies" function that will be called after an interval (500 milliseconds) that should be enough for the AJAX call to finish.

您可以根据自己的需要,所以你的方法不会永远运行调整这个时候,也ajusttestTimeOut的价值。

You have to adjust this time according to your needs, and also ajust "testTimeOut" value so your methods won't run indefinitely.

请参阅QUnit配置文档的更多细节: http://api.qunitjs.com/QUnit.config /

See QUnit config docs for more details: http://api.qunitjs.com/QUnit.config/

这篇关于测试失败,那么成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆