Mr H
Mr H

Reputation: 5302

Why Spy is not called in the Mock (Angular 4)

Can you please check the test below and see why the spy never gets called? The actual code works fine, however the test is not playing.

If I spyOn something inside the this.apiSrv.fetch then test passes though. Very strange!

thank you in advance.

manager.service.spec.ts

describe('ManagerService', () => {
    
  const mockClass = new MockClass();
  let spy;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        ManagerService,
        { provide: ApiService, useValue: mockClass},
      ]
    });
    spy = spyOn(mockClass, 'fetch');
  });

    fit('should call server', inject([ManagerService], (service: ManagerService) => {
      service.callServer();
      expect(spy).toHaveBeenCalled();
    }));
    
});

manager.service.ts

@Injectable()
export class ManagerService {
  constructor(
    private apiSrv: ApiService,
  ) {}

  public callServer() {
    this.apiSrv.fetch('http://url.com').subscribe( (data) => {
       console.log(data);
    });
  }
}

api.service.ts

export class ApiService {
    
  constructor(private http: Http) {}
    
  fetch(resource: string) {
      return this.http.get(resource).map((response) => response.json());
  }
}

mock.ts

export class MockClass {
    public fetch(): Observable<any> {
        const expectedResult = {
            items: {
                data: [],
            }
        };
        return Observable.of(expectedResult);
    }
}

And the result:

enter image description here

Upvotes: 1

Views: 485

Answers (1)

yurzui
yurzui

Reputation: 214195

You need to make sure that MockClass.fetch medhod is actually called (not just stubbed) because you chain it with .subscribe method otherwise it will cause the error and test won't be passed. So callThrough should be used.

spy = spyOn(mockClass, 'fetch').and.callThrough();
                               ^^^^^^^^^^^^^^^^^^ 
                                   add this

Upvotes: 1

Related Questions