Angular 4单元测试用例:注入路由器模块错误“ReferenceError:路由器未定义”

时间:2018-01-22 11:53:23

标签: angular unit-testing jasmine karma-jasmine

注入函数未加载有效参数

import { TestBed, async, inject, fakeAsync, tick, ComponentFixture, getTestBed } from '@angular/core/testing';
import { RouterTestingModule } from "@angular/router/testing"
import { Router } from "@angular/router";
import { Location } from "@angular/common";
import { DriversModule } from '../../drivers.module';
import { DriverAddComponent } from './driver.add.component';
import { routes, routing } from '../../drivers.routing';
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
import { By } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';
import { DriverService } from '../../services/driver.service';


describe('Add driver Component', () => {
    let component: DriverAddComponent;
    let fixture: ComponentFixture<DriverAddComponent>;
    let routerMock;
    let routerSpy: any;
    let location: Location;

    let getvendorsdatastub: getVendorsDataStub;
    beforeEach(async(() => {
        routerMock = {
            navigate: jasmine.createSpy('navigate')
        };

        TestBed.configureTestingModule({
            imports: [DriversModule, RouterTestingModule.withRoutes(routes)],
            providers: [
                { provide: Router, useValue: routerMock },
                { provide: DriverService, useValue: getVendorsDataStub }
            ],
            schemas: [NO_ERRORS_SCHEMA]
        }).compileComponents();


   })); 
 it('should navigate to the add a new driver page', 
    inject([Router],(router: Router) => {    // <= injecting router error
        const spy = spyOn(router, 'navigate');
        fixture = TestBed.createComponent(DriverAddComponent);
        component = fixture.componentInstance;
        component.goBack();
            expect(router.navigate).toHaveBeenCalledWith(['/addDriver']);
        }));
})

调试代码时,我发现params'Router'没有传入inject函数并抛出错误,因为“ReferenceError:路由器未定义”

enter image description here

堆栈追踪:

`"ReferenceError: Router is not defined↵    at eval (eval at <anonymous> (http://localhost:9876/base/src/test.ts?c92fcd34ca1f3fdca5a758e21439c0cb69837438:38527:5), <anonymous>:1:1)↵    at Suite.<anonymous> (http://localhost:9876/base/src/test.ts?c92fcd34ca1f3fdca5a758e21439c0cb69837438:38527:5)↵    at ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts?7c20fa2756eb46abb635df734b059df04f5129cb:3306:26)↵    at Zone.run (http://localhost:9876/base/src/polyfills.ts?7c20fa2756eb46abb635df734b059df04f5129cb:3056:43)↵    at Suite.<anonymous> (http://localhost:9876/base/src/test.ts?c92fcd34ca1f3fdca5a758e21439c0cb69837438:126680:29)↵    at addSpecsToSuite (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?b92f226f045eefc6b7ea6f5d62253577587c5fc8:994:25)↵    at Env.describe (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?b92f226f045eefc6b7ea6f5d62253577587c5fc8:961:7)↵    at Env.jasmineEnv.(anonymous function) [as describe] (http://localhost:9876/base/src/test.ts?c92fcd34ca1f3fdca5a758e21439c0cb69837438:126657:38)↵    at describe (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?b92f226f045eefc6b7ea6f5d62253577587c5fc8:3999:18)↵    at Object.Array.concat.Object.defineProperty.value (http://localhost:9876/base/src/test.ts?c92fcd34ca1f3fdca5a758e21439c0cb69837438:38487:1)`

帮帮我..有任何建议,谢谢......我是单位测试的新手..

0 个答案:

没有答案