【发布时间】:2018-05-31 14:29:45
【问题描述】:
我正在使用 Angular 版本 6 和 jasmine。
当我运行 ng test --sourceMap=false
我得到的错误是:videojs 未定义
如果可能,我想存根 videojs。 另外,请注意组件运行良好,只是单元测试失败。
Videojs 是我用来显示视频等的 npm 库。
我正在测试的组件:
import { AfterViewInit, Component } from '@angular/core';
declare const videojs: any;
@Component({
selector: 'test-details',
templateUrl: './test-details.component.html'
})
export class TestDetailsComponent implements AfterViewInit {
public videoJSplayer: any;
constructor() {}
ngAfterViewInit(): void {
this.initVideo();
}
public initVideo(): void {
this.videoJSplayer = videojs(
document.getElementById('video_player_id'),
{ controls: true, autoplay: false, preload: 'auto' }
);
}
}
我的单元测试
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestDetailsComponent } from './test-details.component';
declare const videojs: any;
fdescribe('TestDetailsComponent', () => {
let component: TestDetailsComponent;
let fixture: ComponentFixture<TestDetailsComponent>;
beforeEach(
async(() => {
TestBed.configureTestingModule({
imports: [],
declarations: [ TestDetailsComponent ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
providers: [
{ provide: videojs, useValue: {} }
]
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(TestDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(true).toBeTruthy(true);
});
});
【问题讨论】:
标签: angular unit-testing testing jasmine