unit-testing a method that contains a promise in it

8 views
Skip to first unread message

Kiflemariam Andom

unread,
Nov 19, 2019, 8:18:44 AM11/19/19
to Angular and AngularJS discussion

Angular 2. My component contains the following piece of code:

export class SectorComponent implements OnInit {

let myList
= [];
let loading
= false;

refresh
(){
this.loading = true;
const that : any = this;
this.dataService.products().subscribe(products=>{
   
that
.myList= products;
console
.log(that.myList);
that
.loading = false;
});


}

}



the refresh() method is called from a button. Now I want to unit test it. the dataService is mocked to return a set of products. In my test, I want to see the corre

  it("should get products", () => {
    component
.refresh();
    console
.log('total found', component.myList.length)
    expect
(component.myList.length).toEqual(1);
 
});




the test fails because myList is always 1 while console.log(that.myList) returns 1. I am almost sure it is related to the expect() happening before observable is complete. How do I go handle it please?

Kiflemariam Andom

unread,
Nov 20, 2019, 3:01:40 AM11/20/19
to ang...@googlegroups.com
oh never mind, solved it with (done). Cant believe it.

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/db072747-9124-430b-8fde-2d3ff30502fd%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages