mercredi 7 novembre 2018

Angular 6 failing nearly all tests

So I'm an absolute beginner to testing. When I run ng test I get hit with 8 failures with the top one being

AppComponent should create the app

My AppComponent is just this:

<app-nav></app-nav>
<router-outlet></router-outlet>

I think since I'm not calling my first component <app-clients> directly this is causing the problem. But my app functions perfectly fine and will land me on my clients component as the root path.

Would it be best practice to change my html structure or to change my tests? And since I don't understand testing, how would I allow them to accept router-outlet?

app.component.spec.ts:

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        ClientsComponent, SecondComponent, ThirdComponent
      ],
      schemas: [ NO_ERRORS_SCHEMA ],
      imports: [
        RouterTestingModule.withRoutes(
          [
            {
              path: '',
              component: ClientsComponent
            },
            {
              path: 'second',
              component: SecondComponent,
            },
            {
              path: 'third',
              component: ThirdComponent,
            }
          ]
        )]
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
  it(`should have as title 'datatables'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('datatables');
  }));
  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to datatables!');
  }));
});

app-routing.module.spec.ts:

describe('AppRoutingModule', () => {
  let appRoutingModule: AppRoutingModule;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule.withRoutes(
          [
            {
              path: '',
              component: ClientsComponent
            },
            {
              path: 'second',
              component: SecondComponent,
            },
            {
              path: 'third',
              component: ThirdComponent,
            }
            ]
        )]
    });
    appRoutingModule = new AppRoutingModule();
  });

  it('should create an instance', () => {
    expect(appRoutingModule).toBeTruthy();
  });
});

Aucun commentaire:

Enregistrer un commentaire