Ionic Test HttpClient


#1

I’m running into some issue with Ionic Testing (Jasmine/Karma) for a Provider that manage the Request to API.

Somehow, the Test never passes and I get this:
“Expected t({ normalizedNames: Map( ), lazyUpdate: [ Object({ name: ‘Authorization’, value: ‘Basic aW5mb0BwMnBvd2VyLmNvLm56OnRlc3Q=’, op: ‘s’ }) ], headers: Map( ), lazyInit: t({ normalizedNames: Map( ), lazyUpdate: null, headers: Map( ) }) }) to be t({ normalizedNames: Map( ), lazyUpdate: [ Object({ name: ‘Authorization’, value: ‘Basic aW5mb0BwMnBvd2VyLmNvLm56OnRlc3Q=’, op: ‘s’ }) ], headers: Map( ), lazyInit: t({ normalizedNames: Map( ), lazyUpdate: null, headers: Map( ) }) }).”

Could anyone help please?

Here is the Test:

describe('RestProvider',()=>{

let provider:RestProvider;
let httpMock:HttpTestingController;

beforeEach(async(()=>{
    TestBed.configureTestingModule({
        declarations: [RestProvider],

        providers: [
            HttpClient
        ],

        imports: [
            IonicModule.forRoot(MyApp),
            HttpClientTestingModule
        ]
    }).compileComponents();

    provider = TestBed.get(RestProvider);
    httpMock=TestBed.get(HttpTestingController);
}));


afterEach(()=>{
    httpMock.verify();
});



it('should be created',inject([RestProvider],(provider:RestProvider)=>{
    expect(provider).toBeTruthy();
}));


it('should retrieve Customer Number via GET', () => {

    const customerAccount:Account ={email:'bla@test.com',password:'test'};
    const serverResponse = {customer_number:'xxxxxxx62',token:'gdfgdfgdhd'};

    provider.getCustomerNumber(customerAccount.email,customerAccount.password).subscribe(data=>{

        expect(data.length).toBe(1);
        expect(data).toEqual(serverResponse);
    });
    const header = provider.generateAuthHeader(customerAccount.email,customerAccount.password);

    const request = httpMock.expectOne(`${BASE_API_URL}/token`);

    expect(request.request.headers).toBe(header);
    expect(request.request.method).toBe('GET');


    request.flush(serverResponse);
});