I'm hitting an issue when trying to MockBackends with Angular2, SystemJs. I'm wanting to test various states of my service methods, but I keep getting the error (Error: createConnection requires an instance of Request, got [object Object]). I can't see any 404s' from Systemjs either. I tried a unit test straight out of the Angular2 docs as that does not work either:
What else should I check?
-- my test
it('should get a response', () => {
let connection; //this will be set when a new connection is emitted from the backend.
let text; //this will be set from mock response
let injector = ReflectiveInjector.resolveAndCreate([
MockBackend,
BaseRequestOptions,
{provide: Http, useFactory: (backend, options) => {
return new Http(backend, options);
}, deps: [MockBackend, BaseRequestOptions]}]);
let backend = injector.get(MockBackend);
let http = injector.get(Http);
backend.connections.subscribe(c => connection = c);
http.request('https://jsonplaceholder.typicode.com/users').subscribe(res => {
text = res.text();
});
console.log('text: ', text);
connection.mockRespond(new Response({body: [{'id': 1}]}));
expect(text).toBeDefined();
});
-- my service call
return this._http.get(this.url)
.map(res => res.json());
it's
import {ResponseOptions} from '@angular/http';
(...)
connection.mockRespond(new Response(new ResponseOptions({body: [{'id': 1}]})));
the constructor of Response expects it's argument wraped in ResponseOptions ;-)
P.S.: updated the buggy sample in master branch a few days ago but doc still uses an old tagged one. The new Sample can already be found on github: https://github.com/angular/angular/blob/master/modules/@angular/http/testing/mock_backend.ts