0

I have this function on my service.

  addTeamMember(data): Observable<any> {
   if (data && typeof data === 'object') {
    return this.http.post<any>(`${this.baseUrl}/members`, 
     JSON.stringify(data), { headers: this.headers });
   } else {
    return null;
   }

}

and i test this with:

it('should be create a new member', () => {
const data = {
  name: 'Rodolfo Lenin',
  lastName: 'xxxx xxxx',
  rol: 'Front-End',
  gender: 'male'
};
spy = spyOn(teamService, 'addTeamMember');
spy.and.callThrough();
spy.and.callFake(function() {});
spy.and.returnValue(data);
response = teamService.addTeamMember(of(data));
console.log(response, data, 'werr');
// expect(teamService.addTeamMember).toHaveBeenCalled();
expect(data).toEqual(jasmine.any(Object));
expect(response).toEqual(data); });

i dont know why test coverage don't recognize this call enter image description here

koutat cruzado
  • 151
  • 1
  • 3
  • Side note: You should not be writing run time checks for something to be of type object. Instead, you should type your parameter and let the compiler check it for you – Ruan Mendes Dec 26 '18 at 23:43

1 Answers1

0

You are stubbing out your teamMember.addTeamMember that's why it's never being called. You can verify that with console.log statements (or just trust your code coverage tool ;)

You should stub out http.post so that your addTeamMember is actually being tested.

Having said that, your test doesn't make sense:

  • You are passing an observable to your function when it expects an object
  • You are faking your method to return an object instead of an observable

Possible solution

 it('creates a new member', () => {

    const data = {
      name: 'Rodolfo Lenin',
      lastName: 'xxxx xxxx',
      rol: 'Front-End',
      gender: 'male'
    };

    spyOn(httpService, 'post').and.returnValue(of(data));

    teamService.addTeamMember(data).subscribe((responseData) => {
        expect(responseData).toEqual(data);
    });
 });

Note that you should write another test for the case where your service returns null. However, returning null because invalid parameters were passed in is generally considered an anti-pattern. See Is returning null bad design?

Also, there's a different approach suggested by Angular https://angular.io/guide/http#testing-http-requests

And here's a fun conversation about checking parameters https://blogs.msdn.microsoft.com/oldnewthing/20091210-00/?p=15713

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • thank for you answer, i fix the two problems that you list, but still understanding what i need to do, to fix the coverage problem. – koutat cruzado Dec 26 '18 at 21:42
  • Instead of calling `spy.and.callFake(function() {});` (and all the other functions that spy on your own service). You should be calling `spyOn(httpService, 'post');` That way, your function is called, but Angular's HTTP service is faked – Ruan Mendes Dec 26 '18 at 21:55
  • i change that to only `spy = spyOn(teamService, 'addTeamMember'); spy.and.returnValue(of(data));`, and coverage still not working. – koutat cruzado Dec 26 '18 at 22:02