I have been facing an issue while trying to test my component. Even though the component itself works perfectly, the test keeps generating error messages that I am unable to resolve.
Here is the snippet of code that I am attempting 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);
});
}
}
}
Below is the test script that seems to be causing issues:
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);
});
});
The console displays the following error message:
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
I can confirm that the code functions correctly, so the issue appears to lie within the test. Any insights on what could be going wrong?