Jasmine HttpClient spy is returning results too soon

I am trying to add a spy to my httpClient, however they spy runs too soon and the unit test fails with:

ObjectUnsubscribedError: object unsubscribed

This is what the simplified code looks like:

  public async setConfig() {
    const config = await this.getConfigFromDatabase().pipe(first()).toPromise();

  private getConfigFromDatabase() {
    const sub = new Subject<ConfigObject>();
      JSON.stringify(data), { headers }
      .subscribe((r: any) => {
        // Do stuff
    return sub.asObservable();

This is what the test looks like:

  it('should set the config', async () => {
    const httpSpy = spyOn((directive as any).httpClient, 'post').and.returnValue(of({}))

    await directive.setConfig();


I was able to get it working with a callFake and use a timeout, but I need modify it to use returnValues so I can get different results for each call from the httpClient.

    httpSpy = spyOn((directive as any).httpClient, 'post').and.callFake(() => {
      const sub = new Subject();
      setTimeout(() => sub.next({}), 10);
      return sub.asObservable();

Source: Angular Questions