title: how to call API post method in karma unit testing and get response from it in angular 4
body: <p>I am trying to write karma unit test cases in angular 4 to get response from post method. I required data coming from API to write further test cases. My sample code snippet is as follows it run but I am not able to get response.</p>
  
  <p>spec.ts</p>
  
  <pre><code>  describe('Login service : ', () =&gt; {
    let component: LoginComponent;
    let fixture: ComponentFixture&lt;LoginComponent&gt;;
  
  
    beforeEach(async(() =&gt; {
    TestBed.configureTestingModule({
    imports: [
      HttpClientModule,
      HttpClientTestingModule
    ]
    })
    .compileComponents();
    }));
  
  afterEach(inject([HttpTestingController], (backend: HttpTestingController) =&gt; {
  backend.verify();
  }));
  
  
   beforeEach(() =&gt; {
  fixture = TestBed.createComponent(LoginComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
  
  debugElement = fixture.debugElement.query(By.css('#userName'));
  htmlElement = debugElement.nativeElement;
  
   });
  
    it(`should emit 'true' for 200 Ok status `, async(inject([LoginService, HttpTestingController],
  (service: LoginService, backend: HttpTestingController) =&gt; {
    service.login({ userName: 'CF09221601k', password: 'LK6XOMPBRkH0', BrowserInfo: 'chrome 46.0.2490.86' }).subscribe((next) =&gt; {
      expect(next).toBeTruthy();
    });
    backend.expectOne('api/ValidateClientLogin').flush(null,
      { status: 200, statusText: 'Ok' });
  })));
  </code></pre>
  
  <p>loginService.ts</p>
  
  <pre><code>export class LoginService {
  
  constructor(private httpClient: HttpClient) { }
  
  public login(user) {
      return this.httpClient.post(APPCONFIG.WEBSERVICE_URL + APIURL.ValidateClientLogin, user, {observe: 'response'});
  }
  }
  </code></pre>