jeudi 23 août 2018

Testing a typescript function with a subscribe in it

So I'm testing with Jasmine/Karma and am using Angular 4. I'm trying to test a function that looks like this:

enter image description here

As you can see, this.name assignment is not getting touched. I've tried testing with:

it('should make sure name is set', () => {
  component.retrievePageInfo();
  expect(component.name).toBeDefined();
});

but it's undefined as this time. I'm not sure how to wait for it to finish. I've also tried:

it('should make sure name is set', () => {
  component.retrievePageInfo().subscribe(art => {
    expect(component.name).toBe(art["name"]);
  });
});

But I get the error "Property 'subscribe' does not exist on type 'void'." BUT if I change the function to:

retrievePageInfo() {
  return this.httpClient.get("/api/art/" + this.art_id)
  .subscribe(art => {
    this.name = art['name'];
  });
}

I get the error "Property 'subscribe' does not exist on type 'Subscription'." but I need the subscribe here.

Help please!

Aucun commentaire:

Enregistrer un commentaire