我正在处理对路由组件的测试,我总是会得到,我想可能需要导入更多模块或提供一些东西,但我不能设法让它正常工作?下面是错误和代码...
TypeError: Cannot read property 'subscribe' of undefined
at new RouterLinkWithHref (/node_modules/@angular/router/@angular/router.es5.js:5028:1)
at createClass (/node_modules/@angular/core/@angular/core.es5.js:10926:1)
at createDirectiveInstance (/node_modules/@angular/core/@angular/core.es5.js:10751:22)
at createViewNodes (/node_modules/@angular/core/@angular/core.es5.js:12187:34)
at callViewAction (/node_modules/@angular/core/@angular/core.es5.js:12633:1)
at execComponentViewsAction (/node_modules/@angular/core/@angular/core.es5.js:12542:1)
at createViewNodes (/node_modules/@angular/core/@angular/core.es5.js:12214:1)
at callViewAction (/node_modules/@angular/core/@angular/core.es5.js:12633:1)
at execComponentViewsAction (/node_modules/@angular/core/@angular/core.es5.js:12542:1)
at createViewNodes (/node_modules/@angular/core/@angular/core.es5.js:12214:1)
这是测试模块的代码
describe('ItemsComponent', () => {
let component: ItemsComponent;
let fixture: ComponentFixture<ItemsComponent>;
let store: Store<any>;
const mockRouter = {
navigate: jasmine.createSpy('#/items')
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
ReactiveFormsModule,
ItemsRoutingModule,
MenuModule,
MenuBottomModule,
AutoCompleteModule,
HttpModule,
StoreModule.provideStore(reducer),
RouterStoreModule.connectRouter(),
StoreDevtoolsModule.instrumentOnlyWithExtension(),
],
declarations: [
ItemsComponent,
ItemComponent,
ItemEditComponent,
],
providers: [
HistoryService,
CookieService,
ItemsService,
Http,
ConnectionBackend,
RouterModule,
{ provide: Router, useValue: mockRouter },
{ provide: Store, useValue: store },
LoadingService,
{ provide: ActivatedRoute, useValue: mockRouter },
MessageService,
LoginService,
ShopsService,
LocationStrategy,
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ItemsComponent);
component = fixture.componentInstance;
store = fixture.debugElement.injector.get(Store)
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
});
由于
答案 0 :(得分:0)
我今天也遇到同样的问题。您要在某处导入RoutingModule。也许在您自己的模块上有一个导入文件。
删除任何对RoutingModule的导入,仅包括RouterTestingModule。
在我的代码中,由于同时包含了这两个原因,因此出现此错误。
希望有帮助