Angular 4/5 unit test case .toBe in Services

Sangwin Gawande

I am trying to write test case for calling Service and getting response from HTTP. Reference

Now the problem is as following :

expect(data.length).toBe(12); Even though data.length is 100, This code does not throw any exception

it('Should call HTTP!', inject([SampleDataService], (sampleDataService) => {
  expect(100).toBe(12); //<== This gives me correct exception i.e Expected 100 to be 12.
  sampleDataService.getData().subscribe(
    (data) => {
      console.log('ngOnInit', data.length); // here data.length is 100
      expect(data.length).toBe(12); // This does not throw any exception
    });
}));

SampleDataService.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class SampleDataService {

  constructor(private http:HttpClient) { }

  getData(){
    return this.http.get("http://jsonplaceholder.typicode.com/posts");
  }
}

app.component.spec.ts

import { TestBed, async, ComponentFixture,inject } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { SampleDataService } from './services/sample-data.service';
import { HttpClientModule,HttpClient } from '@angular/common/http';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        HttpClientModule,
        FormsModule,
        ReactiveFormsModule
      ],
      declarations: [
        AppComponent
      ],
      providers:[SampleDataService]
    }).compileComponents();
  }));
  it('Should call HTTP!', inject([SampleDataService], (sampleDataService) => {
    expect(100).toBe(12);
    sampleDataService.getData().subscribe(
      (data) => {
        console.log('ngOnInit', data.length);
        expect(data.length).toBe(12);
      });
  }));
});
JB Nizet

You typically want to mock Http calls during unit tests. But if really you don't want to, then since http is asynchronous, you need to only end the test once you received the response:

  it('Should call HTTP!', (done) => {
    const service = TestBed.get(SampleDataService);
    service.getData().subscribe(
      (data) => {
        console.log('ngOnInit', data.length);
        expect(data.length).toBe(12);
        done();
      });
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Additional dependencies on other services in angular unit test

From Dev

Angular unit test case for @HostListener escape key event

From Dev

Unit Test Azure Cloud Services

From Dev

Unit Test case using Assert

From Dev

Unit test case for html element

From Dev

Unit test an edge case with Junit

From Dev

Angular 2 and unit testing with services

From Dev

Angular 2 and unit testing with services

From Dev

angular unit test - controller

From Dev

AngularJS override (mock) services for unit test

From Dev

Windows Phone 8, location services and Unit Test

From Dev

AngularJS override (mock) services for unit test

From Dev

Angular 2 and karma-jasmine unit test case to open modal on click of link

From Dev

Can't bind to '(ngModel' since it isn't a known property of 'input' in angular unit test case

From Dev

How to write unit test case for submitting form with drop-down box list in Angular?

From Dev

How to write unit test case for BadRequest?

From Dev

Unit test case view controller crash swift

From Dev

Unit test case for call back methods ios

From Dev

Jasmine unit test case for $routeChangeStart in AngularJS

From Dev

Force a unit test case to ERROR in Python

From Dev

Null @Autowired beans in unit test case

From Dev

Scala : How to create Unit test case in Intellij

From Dev

Mocking Network response for unit test case

From Dev

Controllers are not defined in karma unit test case

From Dev

Unit Test case in Grails with mocking the methods

From Dev

write unit test case in python for below algorithm

From Dev

How to fix the unit test issue in my case?

From Dev

How to cover setInterval in the unit test case in javascript

From Dev

Unit test $formatters in Angular directive

Related Related

HotTag

Archive