I have a angular 2 tool to monitor servers and just started with the tests. When I try to mock the httpService I didnt know how to mock the Rest-API so I looked online, fixed some errors and am now stuck on this one.
Here 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 Testcase:
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 :)
Your syntax seams to be wrong, check docs . Something like this should work:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: Http, useValue: mockHttpProvider },
MockBackend,
BaseRequestOptions]
})
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.