带有异步调用的Jasmine测试事件

带有异步调用的Jasmine测试事件,jasmine,twitter-flight,Jasmine,Twitter Flight,问题是使用异步内部方法测试事件处理程序,该方法由类似facebook的SDK执行 普通测试是: describe('Listens to someevent', function () { it('and triggers anotherevent', function () { var eventSpy = spyOnEvent(document, 'anotherevent'); var data = {

问题是使用异步内部方法测试事件处理程序,该方法由类似facebook的SDK执行

普通测试是:

describe('Listens to someevent', function () {


       it('and triggers anotherevent', function () {
            var eventSpy = spyOnEvent(document, 'anotherevent');
            var data = {
              param1: 'param1',
              param2: 'param2',

            }
            this.component.trigger('somevent', data);

            runs(function() {
                expect(eventSpy).toHaveBeenTriggeredOn(document);
            });

        });
      });
使用选项触发someevent时,将触发组件处理程序:

this.handler = function (e, data) {

     SDK.apicall(data, function (err, response) {

                    if (!err) {
                        doSomething();
                    }

                    // trigger data event
                    that.trigger(document, 'anotherevent');

                });

            }
            ;

        };

在jasmine 1.3及之前的版本中,运行时没有前面的waitsFor,仍然会立即执行。真正使规范异步化的是waitsFor。这件事已经发生了

或者,如果您真的不想在测试期间调用外部API。你可以用类似的东西。这将允许您在测试中立即返回ajax调用,并使用您想要测试的任何响应。这样做的好处是使您的规范更快,无需等待API,并且使您的规范在运行时对API的依赖性更小