I have an angular 2 tool for monitoring servers and just started with tests. When I try to mock the httpService, I did not know how to mock the Rest-API, so I looked online, fixed some errors and now got stuck on it.
Here's the error:
Chrome 53.0.2785 (Windows 10 0.0.0) HttpServiceFront should use an HTTP call Servers FAILED Error: No provider for HttpServiceFront! at NoProviderError.Error (native) ... at drainMicroTaskQueue (webpack:///~/zone.js/dist/zone.js:368:0 <- config/karma-test-shim.js:6854:36) Chrome 53.0.2785 (Windows 10 0.0.0): Executed 2 of 3 (1 FAILED) (skipped 1) (0.268 secs / 0.057 secs)
Here is my test file:
import { ResponseOptions, Response, Http, BaseRequestOptions, RequestMethod } from '@angular/http'; import { TestBed, fakeAsync, inject } from '@angular/core/testing'; import { HttpServiceFront } from '../app/services/httpServiceFront'; import { MockBackend, MockConnection } from '@angular/http/testing'; const mockHttpProvider = { deps: [ MockBackend, BaseRequestOptions ], useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => { return new Http(backend, defaultOptions); } }; describe('HttpServiceFront', () => { beforeEach(() => { {Http, mockHttpProvider} TestBed.configureTestingModule( [MockBackend, BaseRequestOptions] ) }); it('should use an HTTP call Servers', inject( [HttpServiceFront, MockBackend], fakeAsync((service: HttpServiceFront, backend: MockBackend) => { backend.connections.subscribe((connection: MockConnection) => { expect(connection.request.method).toBe(RequestMethod.Get); expect(connection.request.url).toBe( 'http://localhost:8080/server'); }); service.getServers(); }))); });
Thanks for the help:)
source share