Alternative HTTP Mocking Strategy
An alternative to using MockBackend
is to create our own light mocks. Here we create an object and then tell TypeScript to treat it as Http
using type assertion. We then create a spy for its get
method and return an observable similar to what the real Http
service would do.
This method still allows us to check to see that the service has requested the right URL, and that it returns that expected data.
wikisearch.spec.ts
import {
fakeAsync,
inject,
TestBed
} from '@angular/core/testing';
import {
HttpModule,
Http,
ResponseOptions,
Response
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import {SearchWiki} from './wikisearch.service';
const mockResponse = {
"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
},
"query": {
"searchinfo": {
"totalhits": 36853
},
"search": [{
"ns": 0,
"title": "Stuff",
"snippet": "<span></span>",
"size": 1906,
"wordcount": 204,
"timestamp": "2016-06-10T17:25:36Z"
}]
}
};
describe('Wikipedia search service', () => {
let mockHttp: Http;
beforeEach(() => {
mockHttp = { get: null } as Http;
spyOn(mockHttp, 'get').and.returnValue(Observable.of({
json: () => mockResponse
}));
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{
provide: Http,
useValue: mockHttp
},
SearchWiki
]
});
});
it('should get search results', fakeAsync(
inject([SearchWiki], searchWiki => {
const expectedUrl = 'https://en.wikipedia.org/w/api.php?' +
'action=query&list=search&srsearch=Angular';
searchWiki.search('Angular')
.subscribe(res => {
expect(mockHttp.get).toHaveBeenCalledWith(expectedUrl);
expect(res).toEqual(mockResponse);
});
})
));
});