mardi 19 septembre 2017

Karma/jasmine No provider for Http when using httpclientmodule

I am trying to migrate my tests to use the new HttpClientmodule instead of the old Httpmodule.

I keep getting the error Error: No provider for Http! for my service tests.

Here is my testbed.

import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HttpClientModule} from '@angular/common/http'
import { AuthService } from './auth.service';
import { AppConfig } from './app.config';
import { APP_INITIALIZER } from '@angular/core';
import {
    Http,
    ConnectionBackend,
    BaseRequestOptions,
    Response,
    ResponseOptions
} from '@angular/http';

export function initConfig(config: AppConfig) {

    return () => config.loadForTest();
}


describe('AuthService', () => {

    beforeEach(() => TestBed.configureTestingModule({
        imports: [HttpClientTestingModule, HttpClientModule],
        providers:[
        AppConfig,
    HttpClientModule,
            {
            provide: APP_INITIALIZER,
            useFactory: initConfig,
            deps: [AppConfig],
            multi: true
        },
            AuthService,]
    }));

    it('should get user', () => {
        debugger;
        let authService = TestBed.get(AuthService);
        const http = TestBed.get(HttpTestingController);
        // fake response
        const expectedUser = {
            username: 'username' };

        let actualUser = {};
        authService.GetActiveProfile().subscribe((user) => {
            actualUser = user;
        });
        let baseuri = window.location.origin;
        let requesturi = '/user/activeprofile';
        http.expectOne(baseuri + requesturi).flush(expectedUser);

        expect(actualUser).toEqual(expectedUser);
    });
});

Do I need to also implement the old http? From what I could see form the guides that I have read from migrating from http -> httpclient this should not be the case?

Aucun commentaire:

Enregistrer un commentaire