且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

测试失败,那么成功

更新时间:2023-11-23 16:26:58

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

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/