How to do unit test for Http-receiving MockBackend in Angular2?
I am having trouble testing my http unit test. Every time I look at MockBackend , it seems confusing, a lot of code and some imports never work.
I just need a very simple http get unit test
I use: typescript, angular2, jasmine and karma runner.
My actual code is working fine.
Here is my code that I am testing:
import {Injectable} from 'angular2/angular2'; import {HTTP_PROVIDERS, Http, Headers} from 'angular2/http'; @Injectable() export class FirebaseService{ headers: Headers; //Test issue seems to be here when I inject Http instance. constructor(public http?: Http) { this.headers = new Headers(); this.headers.append('Content-Type', 'application/json'); } //This is the method I'm testing. public getSpotifyTracks = ():Promise<Object> =>{ return this.http .get('https://api.spotify.com/v1/tracks/0eGsygTp906u18L0Oimnem', {headers:this.headers}) .map((response) => { return response.json() }).toPromise(); } }
Here is my unit test for this code:
import {it, iit, describe, expect, inject, injectAsync, beforeEachProviders, fakeAsync, tick} from 'angular2/testing'; import {HTTP_PROVIDERS, Http, Headers} from 'angular2/http'; import {FirebaseService} from '../app/firebase-service'; describe('Firebase Service Calls', () => { beforeEachProviders(()=> [Http, FirebaseService]); //Issue seems to be here???? it('get all tracks from spotify', injectAsync([FirebaseService],(service) => { return service.getSpotifyTracks().then((response) => { expect(response.length).not.toBe(null); }); }), 3000); });