は、このような任意のunmocked URLのサーバーにHTTPリクエストを実行するために、実際のバックエンドを使用して、その後、あなたの偽のバックエンドプロバイダへの依存関係として本当のXHRBackend含むことにより、次のとおりです。
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod, XHRBackend } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
export let fakeBackendProvider = {
// use fake backend in place of Http service for backend-less development
provide: Http,
useFactory: (backend: MockBackend, options: BaseRequestOptions, realBackend: XHRBackend) => {
// array in local storage for registered users
let users: any[] = JSON.parse(localStorage.getItem('users')) || [];
// configure fake backend
backend.connections.subscribe((connection: MockConnection) => {
// wrap in timeout to simulate server api call
setTimeout(() => {
// mocked urls handled here ...
// pass through any requests not handled above
let realHttp = new Http(realBackend, options);
realHttp.get(connection.request.url).subscribe((response: Response) => { connection.mockRespond(response); });
}, 500);
});
return new Http(backend, options);
},
deps: [MockBackend, BaseRequestOptions, XHRBackend]
};
詳細とデモについては、this postを参照してください。
投稿を参照してください。 – hugsbrugs