をテスト:RouteLinkが、私はこの方法のようRouteLinkディレクティブを使用するコンポーネントをテストしていangular2
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let el: DebugElement;
let injector: Injector;
let languageService: TranslateService;
let location: Location;
let backend: MockBackend;
let connection: MockConnection; // this will be set when a new connection is emitted from the backend.
let mockRouter: any;
let mockActivatedRoute: any;
const mockBackendResponse = (connection: MockConnection, response: string) => {
connection.mockRespond(new Response(new ResponseOptions({ body: response })));
};
describe('testComponent',() => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
imports: [HttpModule, RouterTestingModule, TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateStaticLoader(http, './assets/language', '.json'),
deps: [Http]
})],
providers: [
{ provide: LanguageService, useClass: LanguageService },
{ provide: BackendLocatorService, useClass: BackendLocatorService },
{ provide: XHRBackend, useClass: MockBackend },
{ provide: APP_BASE_HREF, useValue: '/' }
]
});
injector = getTestBed();
backend = injector.get(XHRBackend);
languageService = injector.get(TranslateService);
location = injector.get(Location);
backend.connections.subscribe((c: MockConnection) => connection = c);
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance; // BannerComponent test instance
// get title DebugElement by element name
});
it('is defined',() => {
expect(TranslateService).toBeDefined();
expect(languageService).toBeDefined();
expect(languageService instanceof TranslateService).toBeTruthy();
});
it('ROUTLINK CLICK',() => {
fixture.detectChanges();
let links = fixture.nativeElement.querySelectorAll('a');
links[1].click();
console.log(links[0]);
expect(location.path()).toBe('/home');
});
});
私は、現在のパスを取得するために<a>
タグを取得し、それをクリックしてみたが、問題があることです現在のパスは「」常に、何:( ここではエラーではありません:?
Chrome 53.0.2785 (Windows 7 0.0.0) testComponent ROUTLINK CLICK FAILED
Expected '' to be '/home'.
任意のアイデア
ありがとうございます – user3518668