mercredi 18 décembre 2019

Angular component testing with jasmine/karma, spyOn

I try to test my component, I know the component works fine but my test gives error messages I wrote this jasmine/karma test that throws an error message that I can't get through.

Here's the code what I want to test:

    export class AddressListComponent implements OnInit {

      paginate: PaginateInterface = new Paginate(Address);
      addresses: AddressInterface[] = [];

      constructor(
        private addressService: AddressService,
        private addressTypeService: AddressTypeService,
        private countryService: CountryService,
        private settlementService: SettlementService,
        private serviceNatureOfTheSite: NatureOfTheSitesService,
        private router: Router,
      ) {
      }

      ngOnInit() {
        if (!this.isBind ) {
          this.addressService.getAll().subscribe( (resolve: PaginateInterface) => {
            this.paginate = resolve;
            this.addresses = this.paginate.data;
          },
          error => {
            throw(error);
          });
        }
      }
    }

And here is the test:

    describe('AddressListComponent', () => {
      const httpClient = new HttpClient(null);
      let injector: Injector;
      let component: AddressListComponent;
      let service: AddressService;
      let settlementService: SettlementService;
      let countryService: CountryService;
      let addressTypeService: AddressTypeService;
      let natureOfTheSiteService: NatureOfTheSitesService;
      const datas = [
        {name: 'test 1'},
        {name: 'test 2'},
        {name: 'test 3'},
      ];
      const paginateData = new Paginate(Address, {
        data: datas,
      });

      beforeEach(() => {
        settlementService = new SettlementService(httpClient, injector);
        countryService = new CountryService(httpClient, injector);
        addressTypeService = new AddressTypeService(httpClient, injector);
        natureOfTheSiteService = new NatureOfTheSitesService(httpClient, injector);
        service = new AddressService(httpClient, injector, settlementService, countryService, addressTypeService, natureOfTheSiteService);
      });

      it('should set address property with the items returned from server', () => {
        spyOn(service, 'getAll').and.returnValue(of(paginateData));

        component = new AddressListComponent(service, addressTypeService, countryService, settlementService, natureOfTheSiteService, null);

        expect(component.addresses.length).toBe(datas.length);
      });
    });

And this error message is thrown back by the console:

    Chrome 79.0.3945 (Windows 10.0.0) AddressListComponent should set address property with the items returned from server FAILED
            Expected 0 to be 3.
                at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/components/address/address-list/address-list.component.spec.ts:49:40)
    Chrome 79.0.3945 (Windows 10.0.0): Executed 1 of 2 (1 FAILED) (0 secs / 0.137 secs)
    Chrome 79.0.3945 (Windows 10.0.0) AddressListComponent should set address property with the items returned from server FAILED
            Expected 0 to be 3.
                at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/components/address/address-list/adChrome 79.0.3945 (Windows 10.0.0): Executed 2 of 2 (1 FAILED) (0.146 secs / 0.138 secs)
    TOTAL: 1 FAILED, 1 SUCCESS
    TOTAL: 1 FAILED, 1 SUCCESS

Is there any idea what's wrong with my test? (I know the code is working fine but the test is failed)

Aucun commentaire:

Enregistrer un commentaire