4
常にコンソールでこのエラーを取得し、ジャスミンを実行する際には、次のコードにカルマをテストし角度2 - HTTP-テスト - エラー:</p> <p>エラー:「ヌルのプロパティを読み取ることができません 『getCookie』」
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()});
}
}
TESTFILE:
//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を』読み取り」ではない
できる理由は何もしませんでしたfinde、またはなぜそれがすべてでそれを読み取ろうと