角度失败:测试路由时无法读取null的属性“ getComponentFromError”

时间:2020-01-15 14:53:46

标签: angular typescript protractor e2e-testing angular-e2e

我正在尝试根据Asim Hussain撰写的《从理论到实践的角度》一书来进行路由测试。但是,此任务失败了,即使它似乎没有在VS Code中引发任何错误,但在运行时引发了这些错误。标题中提到了错误,测试1-2中也提到了- Failed: Cannot read property 'assertPresent' of null。如果您能指导我如何执行所描述的测试,但也不能指导编码的测试,那将是非常棒的。 我的代码如下:

import { AppPage } from './app.po';
import { browser, logging, element } from 'protractor';
import { async, ComponentFixture, fakeAsync, TestBed, tick,
} from '@angular/core/testing';
import {AppComponent} from '../../src/app/app.component';
import {Component} from '@angular/core';
import {RouterTestingModule} from '@angular/router/testing';
import {Routes} from '@angular/router';
import {Router} from '@angular/router';
import { AviorComponent } from '../../src/app/avior/avior.component';

import { DashboardComponent } from '../../src/app/avior/dashboard/dashboard.component';
import { ProfileComponent } from '../../src/app/avior/profile/profile.component';
import { SettingsComponent } from '../../src/app/avior/settings/settings.component';
import { LoginComponent } from '../../src/app/avior/login/login.component';
import { PageNotFoundComponent } from '../../src/app/avior/page-not-found/page-not-found.component';
import { InfoComponent } from '../../src/app/avior/info/info.component';
import { UsersComponent } from '../../src/app/avior/users/users.component';
import { MandatorsComponent } from '../../src/app/avior/mandators/mandators.component';
import { SystemComponent } from '../../src/app/avior/system/system.component';
import { WorkflowsComponent } from '../../src/app/avior/workflows/workflows.component';
import { MessagesComponent } from '../../src/app/avior/messages/messages.component';

import { BrowserDynamicTestingModule,
  platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

const aviorRoutes: Routes = [{
  path: '', component: AviorComponent,
  children: [
    {
      path: '', component: ProfileComponent
    },
    {
      path: 'dashboard', component: DashboardComponent,
      data: {title: 'Dashboard'},
    },
    {
      path: 'login', component: LoginComponent,
      data: {title: 'Login'},
    },
    {
      path: 'logout', component: LoginComponent,
      data: {title: 'Login'},
    },
    {
      path: 'profile', component: ProfileComponent,
      data: {title: 'Profil'},
    },
    {
      path: 'settings', component: SettingsComponent,
      data: {title: 'Einstellungen'},
    },
    {
      path: 'info', component: InfoComponent,
      data: {title: 'Info'},
    },
    {
      path: 'users', component: UsersComponent,
      data: {title: 'Benutzer'},
    },
    {
      path: 'workflows', component: WorkflowsComponent,
      data: {title: 'Workflows'},
    },
    {
      path: 'system', component: SystemComponent,
      data: {title: 'System'},
    },
    {
      path: 'mandators', component: MandatorsComponent,
      data: {title: 'Mandanten'}
    },
    {
      path: 'messages', component: MessagesComponent,
      data: {title: 'Nachrichten'}
    },
    {
      path: '404', component: PageNotFoundComponent,
      data: {title: 'Page not found'},
    }
   ]
}];

describe('Avior App', () => {
  let page: AppPage;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule.withRoutes(aviorRoutes)],
      declarations: [
        DashboardComponent,
        ProfileComponent,
        SettingsComponent,
        LoginComponent,
        PageNotFoundComponent,
        InfoComponent,
        UsersComponent,
        MandatorsComponent,
        SystemComponent,
        WorkflowsComponent,
        MessagesComponent
    ]
   });
    page = new AppPage();

    this.router = TestBed.get(Router);
    location = TestBed.get(Location);

    this.fixture = TestBed.createComponent(AppComponent);
    this.router.initialNavigation();
  });

  it('navigate to "" redirects you to /avior/login', fakeAsync(() => {
      this.router.navigate(['']);
      tick();
      expect(location.path()).toBe('/avior/login');
  }));

  it('navigate to "dashboard" takes you to /dashboard', fakeAsync(() => {
       this.router.navigate(['dashboard']);
       tick();
       expect(location.path()).toBe('avior/dashboard');
  }));


  it('should display Anmeldung message in the header', () => {
    page.navigateTo();
    expect(page.getTitleText()).toEqual('Anmeldung');
  });

  it('there should be username and password login fields', () => {

  });

  it('login using false credentials should make you stay put and throw an error', () => {

  });

  it('login using Chad:chadchad should redirect to /avior/dashboard', () => {

  });

  afterEach(async () => {
    // Assert that there are no errors emitted from the browser
    const logs = await browser.manage().logs().get(logging.Type.BROWSER);
    expect(logs).not.toContain(jasmine.objectContaining({
      level: logging.Level.SEVERE,
    } as logging.Entry));
  });
});


PS This answer仅给我带来了更多错误。

0 个答案:

没有答案
相关问题