我有一个设置 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?我AudioPlayer.context.close()
在 afterEach 块中尝试过,但似乎没有工作。
服务看起来有点像这样:
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/