代码之家  ›  专栏  ›  技术社区  ›  Gerald Hughes

“TypeError:无法读取角6的Karma Jasmine上的属性'routeConfig'为空”

  •  0
  • Gerald Hughes  · 技术社区  · 8 年前

    我得到的错误是“TypeError:无法读取null的属性'routeConfig'”

    有人面临这个问题吗?

    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    
    import { MyComponent } from './my.component';
    import { MyService } from '../services/my-service/my.service';
    import { RouterTestingModule } from '@angular/router/testing';
    
      // mock the service
      class MockMyService extends MyService {
        // mock everything used by the component
      };
    
    describe('MyComponent', () => {
      let component: MyComponent;
      let fixture: ComponentFixture<MyComponent>;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          imports: [RouterTestingModule],
          declarations: [ MyComponent ],
          providers: [{
            provide: MyService,
            useClass: MockMyService
          }]
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(MyComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    支链成分

    @Component({
      selector: "app-action-bar",
      templateUrl: "./action-bar.component.html",
      styleUrls: ["./action-bar.component.css"]
    })
    export class MyComponent implements OnInit, OnDestroy {
      subscription: Subscription;
      buttons: ActionBarButton[];
      messages: string[] = [];
      componentId: number;
    
      constructor(
        private myService: MyService,
        private router: Router
      ) {
        this.componentId = (this.router.routerState.root.firstChild.routeConfig
          .data as PageViewModel).Id;
        this.buttons = new Array<ActionBarButton>();
        this.subscription = this.myService.actionBarShow$.subscribe(data => {
          this.setButtons(data);
        });
      }
    
      ngOnInit() {
        this.messages = this.myService.getMessages();
      }
    
      ngOnDestroy() {
        this.subscription.unsubscribe();
      }
    
      setButtons(data) {
        this.buttons.push(this.createButton(data));
      }
    
      createButton(params?: IActionBarButton) {
        let button = new ActionBarButton();
        button.componentId = params.componentId || button.componentId;
        button.type = params.type || button.type;
        button.label = params.label || button.label;
        button.styleClass = params.styleClass || button.styleClass;
        button.func = params.func || button.func;
        return button;
      }
    
      doAction(button: IActionBarButton) {
        button.func();
      }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   user4676340 user4676340    8 年前

    你的错误来自这里:

    this.router.routerState.root.firstChild.routeConfig.data
    

    您确实已经导入了路由器测试模块,但不要期望它能够满足您的特殊需要。

    beforeEach 功能:

    RouterTestingModule.withRoutes([] as Route[], {} as ExtraOptions);