如何对Angular 2路线进行单元测试firstChild

时间:2018-06-28 12:35:32

标签: angular typescript angular-material2

在我的Angular 4中,我有这段代码。

我想对ngOnInit()进行单元测试,但没有为this.route.firstChild得到任何解决方案。

它显示错误为无法读取未定义的属性firstChild。

ngOnInit() {
    try {

         /*Subscription for routing outlet*/
        this.route.firstChild.queryParams.takeUntil(this.ngUnsubscribe).subscribe(params => {
            if (this.ViewMode == 'single' && params.ViewMode == 'Grid') {
                this.grid = (params.ViewMode == 'Grid' ? true : false);
                this.ActivaPageNo = 0;
                this.GridPagination(this.fdcTreeData, 4, 0, 'Data');
            }
            this.ViewMode = params.ViewMode;
        });
        this.grid = (this.ViewMode == 'Grid' ? true : false);
        if (this.grid == false) {
            this.singleView()
        }
        this.fdcTreeData = this.commonService.FDCMainLocationsForForms;
        this.date = new FormControl(new Date());
        if (this.date) {
            this.commonService.fdcDate = "" + this.date.value.getDate() + (this.date.value.getMonth() + 1) + this.date.value.getFullYear();
        }
    } catch (e) {
        console.error('Exception in ngOnInit() of  FDCGridTemplateComponent at time ' + new Date().toString() + '. Exception is : ' + e);
        this.logger.logException(new Error('Exception in ngOnInit() of FDCGridTemplateComponent at time ' + new Date().toString() + '. Exception is : ' + e));
    }  
}

这是我的测试用例

describe('FdcgridTemplateComponent',()=> {

let Excomponent: FdcgridTemplateComponent;
let fixture: ComponentFixture<FdcgridTemplateComponent>;
let dialog: MatDialog;
let commonservice: CommonService;
let fdcservice: FDCService;
let mockRouter: any;
let mockActivatedRoute: any;
beforeEach(async(() => {

    mockRouter = sinon.createStubInstance(Router);
    mockActivatedRoute = sinon.createStubInstance(ActivatedRoute);
    TestBed.configureTestingModule({
    declarations: [FdcgridTemplateComponent],
    imports: [OverlayModule, 
        HttpModule, RouterTestingModule,
        FormsModule, ReactiveFormsModule,
        BrowserAnimationsModule, MatDialogModule],
        providers: [MatDialog, CommonService, Http,  ConnectionBackend, ActivitiesService, CollaborationService, FDCService, MessagingService, AppInsightsService,
            { provide: 'instrumentationKey', useValue: '36935597-7d97-4635-ab0b-409a8c6e4223' },
            { provide: 'BASE_URL', useValue: 'http://localhost' },
            { provide: 'BASE_URL', useValue: "http://localhost" },
            { provide: ComponentFixtureAutoDetect, useValue: true },
            { provide: MatDialogRef, useValue: { close: (dialogResult: any) => { } } },
            { provide: MAT_DIALOG_DATA, useValue: [] },
            { provide: ActivatedRoute, useValue: mockActivatedRoute },
            { provide: Router, useValue: mockRouter },
        ComponentFactoryResolver, FdcgridTemplateComponent],
        schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
    }).compileComponents();
  dialog = TestBed.get(MatDialog);
}));
beforeEach(inject([FdcgridTemplateComponent], (component) => {
    Excomponent = component;
}));
beforeEach(inject([CommonService], Cservice => {
    commonservice = Cservice;
}));
beforeEach(inject([FDCService], Fservice => {
    fdcservice = Fservice;
}));

describe('#ngOnInit', () => {
    it('should ...', () => {
        mockActivatedRoute.snapshot = { params: { routeParam1: 'grid', routeParam2: 'Single' } };
        mockRouter.currentRouterState = { snapshot: { queryParams: { viewPage: 'Single' } } };
        Excomponent.ngOnInit();
    });
});

});

0 个答案:

没有答案
相关问题