我有一个设置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/
希望这可以帮助某人。
关于javascript - 如何在Jasmine测试中发布AudioContext,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32721728/