mardi 28 juin 2016

angular2 testing framework async testing issue

I have created angular 2 a project and a service with angular-cli and trid to test my service.

But API does not fail in async function although it should fail ;moreover, it just ignores those exceptions.

/* tslint:disable:no-unused-variable */

import {
  beforeEach, beforeEachProviders, describe, xdescribe,
  expect, it, xit, async, inject, injectAsync
} from '@angular/core/testing';
import { SearchService } from './search.service';
import {provide} from '@angular/core';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {XHRBackend, Response, ResponseOptions, HTTP_PROVIDERS} from '@angular/http';

describe('Search Service', () => {
  let searchService: SearchService;
  let mockBackend: MockBackend;

  beforeEachProviders(() => [
    HTTP_PROVIDERS,
    MockBackend,
    provide(XHRBackend, { useClass: MockBackend }),
    SearchService
  ]);

  beforeEach(injectAsync([SearchService, MockBackend], (s, m) => {
    searchService = s;
    mockBackend = m;
  }));

  it('async test', () => {
    setTimeout(() => {
      expect(2).toBe(1);
    }, 3000);
  });

It just ignores those minimal test case.

Then I have read some doc and updated my code as follow.

it('async test with done', (done) => {
  setTimeout(() => {
    expect(1).toBe(1);
    done();
  }, 1000);
});

But this time, the test fails although it should pass. The error is as follow.

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

I change default timeout value to bigger value but it is no effect.

Aucun commentaire:

Enregistrer un commentaire