Angular 2 - http测试-错误:"Cannot read property 'getCoo
Angular 2 - Http-Testing - Error: "Cannot read property 'getCookie' of null"
当对以下代码运行带有karma的Jasmine测试时,总是在控制台中得到此错误:
错误:服务:TypeError: Cannot read property 'getCookie' of null
//http.service.ts
import {Injectable, Inject, ReflectiveInjector} from '@angular/core';
import {Headers, Http, Response, HTTP_PROVIDERS} from '@angular/http';
import {Observable} from "rxjs";
const injector = ReflectiveInjector.resolveAndCreate(HTTP_PROVIDERS);
const http = injector.get(Http);
@Injectable()
export class HttpService {
constructor() {}
httpTest(){
return http.get("https://jsonplaceholder.typicode.com/users")
.map(response =>{ return response.json()});
}
}
测试文件:
//http.service.spec.ts
import {
it,
describe,
expect,
inject,
addProviders,
beforeEach
} from '@angular/core/testing';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {provide} from '@angular/core';
import {
Http,
BaseRequestOptions,
Response,
ResponseOptions
} from '@angular/http';
import { HttpService } from './http.service';
describe('Http-Test', () => {
beforeEach(() => {
addProviders([
HttpService,
BaseRequestOptions,
MockBackend,
{
provide: Http,
useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
}
])
});
beforeEach(inject([MockBackend], (backend: MockBackend) => {
const baseResponse = new Response(new ResponseOptions({ body: 'userListAsJSON' }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
}));
it('should return response when subscribed to httpTest',
inject([HttpService], (testService: HttpService) => {
testService.httpTest().subscribe((res: Response) => {
expect(res.text()).toBe('userListAsJSON');
});
})
);
})
没有找到任何原因,为什么它不能"读取属性'getCookie'的null",或者为什么它试图读取它…
这是XSRFStrategy provider的一个问题。
如果您在测试文件中创建一个假的XSRFStrategy并将其添加为提供者,则
可以工作。class FakeXSRFStrategy implements XSRFStrategy {
public configureRequest(req: Request) { /* */ }
}
const XRSF_MOCK = provide(XSRFStrategy, { useValue: new FakeXSRFStrategy() })
相关文章:
- 未捕获TypeError: Cannot read property 'geocode'未定义的'
- SailsJs中的错误.TypeError: Cannot read property '的定义
- 未捕获的TypeError: Cannot read property 'top'的定义
- 未捕获的TypeError: Cannot read property '未定义的数据表
- 未捕获的TypeError: Cannot read property 'timing'的定义
- 未捕获的TypeError: Cannot read property 'checked'零
- 未捕获的TypeError: Cannot read property 'top'在引导中单击导航栏时未
- “未捕获的TypeError: Cannot read”属性是什么?JS误差均值
- Extjs5 treepanel [Uncaught TypeError: Cannot read property &
- TypeError: Cannot read property 'submit'添加到购物车'
- 添加自定义jquery.验证方法导致Uncaught TypeError: Cannot read property &
- Vue 2和Vue- resource [Cannot read property 'get'未定义的(
- asp:textbox Uncaught TypeError: Cannot read property 'va
- TypeError: Cannot read property 'search' of undefine
- 读取json时出错未捕获类型错误:Cannot read property 'output'的定义
- 未捕获的TypeError: Cannot read property 'current'在选择任何选项
- 未捕获的TypeError: Cannot read property '3'的定义
- jquery-ui.js:12443未捕获的TypeError: Cannot read property 'a
- jquery Uncaught TypeError: Cannot read property 'left
- 未捕获类型错误:Cannot read property 'print'的定义