繁体   English   中英

如何在Jasmine测试中发布AudioContext

[英]How to Release AudioContext in Jasmine Tests

我有一个设置audioContext的Angular服务。 Jasmine会为每个测试创建一个新服务,因此在6个测试之后,所有测试都会失败并显示以下错误:

Error: Failed to construct 'AudioContext': The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6).

我有办法在两次测试之间清除AudioContext吗? 我已经在afterEach块中尝试过AudioPlayer.context.close() ,但似乎没有用。

服务看起来像这样:

angular.module('myApp')
  .service('AudioPlayer', function () {

    var self = this;

    self.context = new AudioContext();

    this.doSomething = function () {
       // doing super cool testable stuff here
    }
  })

测试看起来像这样:

describe('AudioPlayer', function () {
  var AudioPlayer;

  beforeEach(function () {
    inject(function ($injector) {
      AudioPlayer = $injector.get('AudioPlayer');
    });
  });

  afterEach(function () {
    AudioPlayer.context.close();
  });

  it('does cool stuff', function () {
    AudioPlayer.doSomething();    
    // unit test
  });

  it('does other cool stuff', function () {
    AudioPlayer.doSomething();    
    // unit test
  });

});

谢谢您的帮助!

这是一个说明问题的jsFiddle: http : //jsfiddle.net/briankeane/cp929can/1/

我最终在测试中创建了类似单例的上下文,然后使用返回该相同AudioContext的函数对构造函数进行了定位...这是最终的测试代码:

describe('AudioPlayer', function () {
  var AudioPlayer;
  var context = new AudioContext();     // create the AudioContext once

  beforeEach(function () {
    module('myApp');

    inject(function ($injector) {
      spyOn(window, 'AudioContext').and.callFake(function () {
        return context;                // stub the constructor
      });
      AudioPlayer = $injector.get('AudioPlayer');
    });
  });

  for (var i=0;i<7;i++) { 
      it('does cool stuff', function () {
        AudioPlayer.doSomething();  
        expect(true).toBe(true);
        // unit test
      });
  }
});

这是工作中的小提琴: http : //jsfiddle.net/briankeane/3ctngs1u/

希望这可以帮助某人。

您可以关闭它。

    audioCtx.close();

参见文档audioContext.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM