Currently, I am experimenting with Jasmine/Karma while working on an Angular 4 project. The issue I'm facing involves testing a function that seems to have trouble setting the 'name' property:
https://i.stack.imgur.com/3q49i.jpg
The assignment of this.name appears to be untouched. Here are the different approaches I've attempted so far:
it('should make sure name is set', () => {
component.retrievePageInfo();
expect(component.name).toBeDefined();
});
Unfortunately, at this point in time, 'name' remains undefined. I also tried:
it('should make sure name is set', () => {
component.retrievePageInfo().subscribe(art => {
expect(component.name).toBe(art["name"]);
});
});
However, this led to the error "Property 'subscribe' does not exist on type 'void'." Modifying the function like this:
retrievePageInfo() {
return this.httpClient.get("/api/art/" + this.art_id)
.subscribe(art => {
this.name = art['name'];
});
}
Brought the error "Property 'subscribe' does not exist on type 'Subscription'." even though subscribe is necessary here.
I would greatly appreciate some guidance on how to overcome this hurdle!
Edit: I'd find a basic example helpful since my attempts so far haven't been successful. My current approach involves splitting the function into one that returns an observable and another that utilizes it, but my tests continue to fail to capture the desired outcome.
Edit: In my latest try, I have imported fakeAsync and tick
it('should set name value', fakeAsync(() => {
expect(component.name).toBeUndefined();
spyOn(component, "retrievePageInfo");
component.retrievePageInfo();
tick();
fixture.detectChanges();
expect(component.name).toBeDefined();
}));
Yet, despite these efforts, the failure persists with "Expected undefined to be defined."