Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jasmine 2.0中无法设置回调时的异步测试_Javascript_Asynchronous_Jasmine - Fatal编程技术网

Javascript Jasmine 2.0中无法设置回调时的异步测试

Javascript Jasmine 2.0中无法设置回调时的异步测试,javascript,asynchronous,jasmine,Javascript,Asynchronous,Jasmine,Jasmine 2.0中对异步测试的更改在许多方面都是巨大的。然而,当我无法将回调绑定到异步方法时,我不确定我是否完全理解如何测试异步代码 如何针对Jasmine 2.0中数量未知的异步事件进行测试?或不提供回调调用的并行异步事件 在1.3中,我将这样做: describe("my spec", function () { it("should check the state of a variable after unknown number of async events", func

Jasmine 2.0中对异步测试的更改在许多方面都是巨大的。然而,当我无法将回调绑定到异步方法时,我不确定我是否完全理解如何测试异步代码

如何针对Jasmine 2.0中数量未知的异步事件进行测试?或不提供回调调用的并行异步事件

在1.3中,我将这样做:

describe("my spec", function () {
   it("should check the state of a variable after unknown number of async events", function () {

        // This will execute several async functions in parallel (more than 1).
        // Once they are all complete the 'window.done' variable will be set to "true".
        // This method does not provide a callback.
        fire_parallel_async_methods();

        waitsFor(function () {
            // I know that once this condition is met,
            // all the above async calls are done
            return window.done === true;
        });

        runs(function () {
            // Now I can run the rest of my unit tests
        });
   });
});
托尼的答案是唯一的解决办法吗

另一个用例用于事件验证。例如:

describe("my spec", function () {
   it("should make sure event is fired", function () {

        // This element has an event binding on 'click' which changes its class.
        // In this test I want to check to make sure the event does the right thing.
        var $element = $('.some-element');

        // Simulate a click
        $element.simulate('click');
        // or $element.click();

        waitsFor(function () {
            // Once the event is done, the element should have a new 'after-event' class
            return $element.hasClass('after-event');
        });
   });
});

在本例中,我无法访问事件的绑定,因此无法向其附加回调。我如何在Jasmine 2.0中验证这一点?

我能够找到一种使用超时的解决方法,它模仿waitsFor()行为:

describe("my spec", function () {
   it("should make sure event is fired", function (done) {

        // This element has an event binding on 'click' which changes its class.
        // In this test I want to check to make sure the event does the right thing.
        var $element = $('.some-element');

        // Simulate a click
        $element.simulate('click');
        // or $element.click();

        // Set a timeout to wait for the async event to go through.
        // Pick a time here that will be enough. It's a little messy, but it works for cases
        // where you can't attach a callback. The hardest part is figuring out how long to set
        // the timeout for.
        setTimeout(function () {
            // Do your test here
            expect($element).toHaveClass('after-event');

            // Tell Jasmine the test is finished
            done();
        }, 500);
   });
});

如果不能将回调绑定到异步方法,那么可能只需要猜测并设置超时。为什么不使用像Q这样的异步库来包装所有请求并准确地知道它们何时完成?在某些情况下,异步事件都在第三方模块内(例如),这不是一个选项。您可以为该第三方进程制作一个包装器,使其与Q或类似库配合使用。否则,除了根据您认为需要多长时间使用超时之外,您真的无能为力。