My service involves making 2 method calls in the constructor:
constructor(private http: HttpClient) {
this.apiURL = environment.apiURL;
this.method();
this.method2().subscribe();
}
I am facing difficulties testing this service in the TestBed. I'm unable to spyOn using Jasmine's spies before calling TestBed.get(MyService)
.
Below is my Service's configuration for the TestBed:
let myService: MyService;
let backend: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule,
HttpClientTestingModule
],
providers: [
MyService
]
}).compileComponents();
myService = TestBed.get(MyService);
backend = TestBed.get(HttpTestingController);
How can I properly spyOn method()
and method2()
and return a value for them? Is it not recommended to include methods in the constructor of a Service?
method()
and method2()
make an HTTP call to the server to "refresh" user data, such as Posts
on a wall. Another method involves an Observable.timer
that auto signs out the app once the token expires.