我有一个Angular2组件,其中包含一个@angular/material
的选项卡控件。Angular2 Material ViewportRuler单元测试错误
我想测试我的组件(见下文简化代码 - 我得到的,有没有点在测试中这是这个简单的一个组成部分),并正在以下错误:
Error: Error in ./MdTabHeader class MdTabHeader - inline template:0:0 caused by: No provider for ViewportRuler!
Error: No provider for ViewportRuler!
我的假设是尝试将ViewportRuler(https://github.com/angular/material2/blob/master/src/lib/core/overlay/position/viewport-ruler.ts)作为提供者。当我做到这一点(见注释以下行),因缘返回:
Uncaught SyntaxError: Unexpected token import
at http://localhost:9876/context.html:10
其中,从钻头谷歌搜索,顾名思义,它的服务当.ts文件浏览器,而不是编译.js文件。我可能从错误的地方引用它。
我的问题是:如何让我的测试编译?
我的代码是:
my.component.ts:
@Component({
selector: 'test',
template: require('./test.component.html')
})
export class TestComponent {
items: any[];
constructor() {
this.items = [{ title: 'test 1', description: 'description 1' }, { title: 'test 2', description: 'description 2' }];
}
}
my.component.html:
<md-tab-group>
<md-tab *ngFor="let link of items">
<template md-tab-label>
<h4>{{link.title}}</h4>
</template>
{{link.description}}
</md-tab>
</md-tab-group>
my.component.spec.ts:
import { TestBed } from '@angular/core/testing';
import { Component} from '@angular/core';
import { MaterialModule } from '@angular/material';
import { ViewportRuler} from '@angular/material/core/overlay/position/viewport-ruler'
import { TestComponent } from './test.component';
describe("TestComponent",
() => {
let fixture, component;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [MaterialModule],
declarations: [TestComponent],
providers: [
//{ provide: ViewportRuler }
]
});
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
it('true = true',() => {
expect(true).toBe(true);
});
});
我试过了尽可能多地包含信息,但我是整个Angular世界的新手,所以让我知道是否还有其他我可以提供的内容。
非常感谢。
您是否在您的测试模块中尝试过使用'MaterialModule.forRoot()'? – stealththeninja
@stealththeninja修复它!写一个答案,我会接受:) – Alex