Mam element Angular2, który zawiera kontrolkę tab z @angular/material
.Angular2 Material ViewportRuler testujący jednostkę błąd
Próbuję przetestować komponent (patrz uproszczony kod poniżej - mam, że nie ma sensu testowania składnik, który to jest proste) i otrzymuję następujący błąd:
Error: Error in ./MdTabHeader class MdTabHeader - inline template:0:0 caused by: No provider for ViewportRuler!
Error: No provider for ViewportRuler!
Moim założeniem było aby spróbować włączyć ViewportRuler (https://github.com/angular/material2/blob/master/src/lib/core/overlay/position/viewport-ruler.ts) jako dostawcę. Kiedy to zrobić (patrz wykomentowane linie poniżej), karma wraca:
Uncaught SyntaxError: Unexpected token import
at http://localhost:9876/context.html:10
, które z odrobiną Googling sugeruje, że jest on służąc plik .ts do przeglądarki, zamiast skompilowanych .js. Możliwe, że odwołuję się do niego z niewłaściwego miejsca.
Moje pytanie brzmi: w jaki sposób mogę wykonać testy?
Mój kod to:
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);
});
});
Próbowałem aby podać jak najwięcej informacji, ale jestem nowy w całym świecie Angular, więc daj mi znać, jeśli jest coś, co mogę ci zapewnić.
Wielkie dzięki.
Czy próbowałeś używać 'MaterialModule.forRoot()' w swoim module testowym? – stealththeninja
@stealththeninja to naprawiło!Napisz odpowiedź, a ja ją przyjmuję :) – Alex