Error: createConnection requires an instance of Request, got [object Object] - Angular2, SytemJs
Error: createConnection requires an instance of Request, got [object Object] - Angular2, SytemJs
我在尝试使用 Angular2、SystemJs 模拟后端时遇到了问题。我想测试我的服务方法的各种状态,但我不断收到错误(错误:createConnection 需要一个请求实例,得到 [object Object])。我也看不到来自 Systemjs 的任何 404。我尝试直接从 Angular2 文档中进行单元测试,因为它也不起作用:
我还应该检查什么?
-- 我的测试
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();
});
-- 我的服务电话
return this._http.get(this.url)
.map(res => res.json());
是
import {ResponseOptions} from '@angular/http';
(...)
connection.mockRespond(new Response(new ResponseOptions({body: [{'id': 1}]})));
Response 的构造函数期望它的参数包含在 ResponseOptions 中;-)
P.S.: 几天前更新了 master 分支中的 buggy 样本,但 doc 仍然使用旧的标记样本。已经可以在 github 上找到新样本:https://github.com/angular/angular/blob/master/modules/@angular/http/testing/mock_backend.ts
我在尝试使用 Angular2、SystemJs 模拟后端时遇到了问题。我想测试我的服务方法的各种状态,但我不断收到错误(错误:createConnection 需要一个请求实例,得到 [object Object])。我也看不到来自 Systemjs 的任何 404。我尝试直接从 Angular2 文档中进行单元测试,因为它也不起作用:
我还应该检查什么?
-- 我的测试
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();
});
-- 我的服务电话
return this._http.get(this.url)
.map(res => res.json());
是
import {ResponseOptions} from '@angular/http';
(...)
connection.mockRespond(new Response(new ResponseOptions({body: [{'id': 1}]})));
Response 的构造函数期望它的参数包含在 ResponseOptions 中;-)
P.S.: 几天前更新了 master 分支中的 buggy 样本,但 doc 仍然使用旧的标记样本。已经可以在 github 上找到新样本:https://github.com/angular/angular/blob/master/modules/@angular/http/testing/mock_backend.ts