如何在Jasmine测试中释放AudioContext
How to Release AudioContext in Jasmine Tests
我有一个Angular服务,它设置了一个audioContext。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块,但似乎不工作。
service看起来像这样:
angular.module('myApp')
.service('AudioPlayer', function () {
var self = this;
self.context = new AudioContext();
this.doSomething = function () {
// doing super cool testable stuff here
}
})
and tests看起来像这样:
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 ()
相关文章:
- 显示:阻止/无html元素,css.not从内存中释放
- MVC和JQuery Mobile:单选按钮在发布时释放状态
- AudioContext on Safari
- 如何在Microsoft Edge中发布AudioContext
- AudioContext从blob获取数据的视频创建MediaElementSource
- ReacJS与猴面包树,如何在卸载组件后释放光标
- Jquery abort()没有'似乎没有完全释放
- 在拖动过程中释放鼠标时忽略 JavaScript mouseUp 事件
- 无法将 createAnalyser() 和 createGain() 连接到 audioContext()
- 节点.js集群模块不会在 http 上释放套接字/端口.Server.close().
- 在 Node 中.js释放 zalgo 的设计模式为什么异步路径是一致的
- 检查是否释放了 HTML 按钮
- 关闭 DIalog 后释放 Java Applet
- 继续功能,直到释放鼠标为止
- 相位器-按键释放
- 在客户端断开连接时释放事件处理程序 Socket.IO
- jQuery UI可排序,释放鼠标按钮事件
- 自定义Cordova插件,释放一个保持回调而不调用它
- 如果释放鼠标时内部元素未悬停,则防止触发“单击”的正确方法
- 如何在Jasmine测试中释放AudioContext