Angular 2 Recipes - Tuong-Nguyen/Angular-D3-Cometd GitHub Wiki

Integration test Service which depends on http

Problem: We need to test a service which uses Http for communicating with server
Solution:

  1. Set up TestBed
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule], // HttpModule
      providers: [TopicApi]  // Service
    }).compileComponents();
  }));
  1. Add asynchronous test
    it('return list topics which has "__consumer_offsets" topic', (done) => {
      // Create service instance from TestBed
      const service = TestBed.get(TopicApi);
      service.getTopics().subscribe((response) => {
          expect(response).toContain('__consumer_offsets');
          done();
        },
        HttpTestErrorHandlers.failOnError);
    });

Note: Do not use async function when testing with Observable. When the test fails, it is reported for another test.

Ignore CORS in Karma tests

Problem: Solution: