【问题标题】:jasmine 2 - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVALjasmine 2 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调
【发布时间】:2015-05-26 23:00:11
【问题描述】:

jasmine 2 遇到问题并连接异步规范:

define(['foo'], function(foo) {
  return describe('foo', function() {
    beforeEach(function(done) {
      window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
      return setTimeout((function() {
        console.log('inside timeout');
        return done();
      }), window.jasmine.DEFAULT_TIMEOUT_INTERVAL);
    });
    return it('passes', function() {
      return expect({}).toBeDefined();
    });
  });
});

当我通过业力运行时,我会回来

错误:超时 - 未在超时内调用异步回调 由 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定。

然后规范失败。我试图覆盖默认超时,但我无法克服错误

【问题讨论】:

    标签: javascript jasmine


    【解决方案1】:

    您使用的超时间隔与 Jasmine 用于超时测试失败的超时间隔相同,即您的超时被触发以使用 Jasmine 的默认间隔触发,这会导致测试失败。

    如果您将超时设置为小于 jasmine 默认超时,则测试通过。

    describe('foo', function () {
        beforeEach(function (done) {
            window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
            setTimeout(function () {
                console.log('inside timeout');
                done();
            }, 500);
        });
        it('passes', function () {
            expect({}).toBeDefined();
        });
    });
    

    见小提琴here

    【讨论】:

    • 你是救生员!
    【解决方案2】:

    我的 2 美分。在另一种情况下,我也收到了问题中提到的这个错误。

    我有一个非常简单的规范,如下所示:

    describe('login feature', function() {
        it('should show the logged in user name after successful login', function(done) {
            expect({}).toBeDefined();
            //done(); // if you don't call this done here, then also above error comes
        });
    });
    

    查看 'it' 中注释掉的 //done() 函数

    【讨论】:

    • 你也可以去掉done参数。那你就不用调用它并保存一行代码了:)
    • 啊。当然,下次我回到 Jasmine 时,我会尝试松开那个.. ;-)
    【解决方案3】:

    另一个可能对您有用的选项是使用async

    async 函数是 Angular 测试实用程序之一,必须导入...它接受一个无参数函数并返回一个函数,该函数成为 beforeEach 的真正参数

    async 参数的主体看起来很像同步 beforeEach 的主体。没有什么明显的异步。例如,它不会返回一个承诺,也没有像标准 Jasmine 异步测试那样调用 done 函数。在内部,async 安排 beforeEach 的主体在隐藏异步执行机制的特殊异步测试区域中运行。

    见:https://angular.io/docs/ts/latest/guide/testing.html#!#async-in-before-each

    describe('Component: MyComponent', () => {
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          providers: [
            {provide: MyService, useValue: MyServiceMock()},
          ]
        })
        // Using webpack through Angular Cli. You may need ".compileComponents()"
        fixture = TestBed.createComponent(MyComponent)
        component = fixture.componentInstance
        // Initialize the component
        component.ngOnInit()
        fixture.detectChanges()
      }))
    
      it('should show the logged in user name after successful login',() {
          expect({}).toBeDefined()
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2018-11-21
      • 2016-04-21
      • 1970-01-01
      相关资源
      最近更新 更多