Angular2,TypeScript,PhantomJS-找不到变量:音频 [英] Angular2, TypeScript, PhantomJS - Can't find variable: Audio
问题描述
PhantomJS不支持HTML5音频标签,这使得很难测试我的Angular2应用.我一直在寻找解决方案,但是不幸的是,我还没有找到一种方法来模拟Audio对象或以某种方式解决这个问题.我有一个看起来像这样的Angular2服务:
PhantomJS doesn't support HTML5 Audio tags, which is making it difficult to test my Angular2 app. I've looked around for a solution, but unfortunately I haven't been able to figure out a way to mock the Audio object or somehow get around this. I have an Angular2 service that looks like this:
import {Injectable} from 'angular2/core';
@Injectable()
export class MyService {
var snd;
constructor() {
this.snd = new Audio('Assets/sound.wav');
}
simpleFunction() {
return true;
}
}
我的测试如下:
import {describe, expect, it} from 'angular2/testing';
import {Component} from 'angular2/core';
import {MyService} from 'my.service';
export function main() {
describe('My service', () => {
var myService: MyService = new MyService();
it('should work', () => {
expect(myService.simpleFunction).toEqual(true);
});
});
}
这当然是简化版本,但是它演示了我遇到的问题.当我运行测试时,我收到此错误:
This is of course a simplified version, but it demonstrates the issue I'm having. When I run my test, I receive this error:
ReferenceError: Can't find variable: Audio
我一直无法弄清楚如何避免这种情况在我的测试中发生.我已经在服务中尝试过此操作:
I haven't been able to figure out how to avert this from happening in my tests. I have tried this in my service:
if (Audio !== undefined)
要检查是否已分配'snd'变量,但是我仍然得到相同的结果.
To check before the 'snd' variable is assigned, but I still got the same result.
推荐答案
我认为您可以像这样模拟缺少的音频构造函数:
I think you could mock missing Audio constructor like this:
import {describe, expect, it} from 'angular2/testing';
import {Component} from 'angular2/core';
import {MyService} from 'my.service';
export function main() {
describe('My service', () => {
beforeEach(() => {
if (!window.Audio) {
window.Audio = function() {
return {
play: function() {},
pause: function() {},
// ... etc
};
};
}
});
var myService: MyService = new MyService();
it('should work', () => {
expect(myService.simpleFunction).toEqual(true);
});
});
}
这篇关于Angular2,TypeScript,PhantomJS-找不到变量:音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!