angular2 karma test for component : Expected null to be truthy

Question!

I had write a component with angular-cli (ng g component)

import .....

@Component({
   moduleId: module.id,
   templateUrl: 'admin.component.html',
   styleUrls: ['admin.component.css'],
   directives: [ApplicationLinkComponent]
 })
  export class AdminComponent {

          constructor() { }

   }

ng g component generates also test file

import .......

describe('Component: Admin', () => {
    let builder: TestComponentBuilder;

beforeEachProviders(() => [AdminComponent]);
beforeEach(inject([TestComponentBuilder], function(tcb: TestComponentBuilder) {
    builder = tcb;
}));

it('should create the component', inject([], () => {
    return builder.createAsync(AdminComponentTestController)
        .then((fixture: ComponentFixture<any>) => {
            expect(fixture).toBeTruthy();
            let query =    fixture.debugElement.query(By.directive(AdminComponent));
            expect(query).toBeTruthy();
            expect(query.componentInstance).toBeTruthy();
        });
}));
});

@Component({
    selector: 'test',
    template: `
    <app-admin></app-admin>
  `,
   directives: [AdminComponent]
})
class AdminComponentTestController {
}

When i run the test , it's false Expected null to be truthy.

These conditions are not thruthy

            expect(query).toBeTruthy();
            expect(query.componentInstance).toBeTruthy();

why ????

By : Florence


This video can help you solving your question :)
By: admin