0
http post serivce를 테스트합니다. 그러나 다음 오류로 인해 테스트가 실패합니다. 예상되는 개체가 Object 일종의 상태 인 Response : URL : null의 경우 null입니다.상태에 대한 응답 : null null : http post service를 테스트 할 때 null erorr
URL을 RequestOptions에 추가하려고 시도했지만 오류가 발생했습니다. 어떻게 내가
// 테스트 파일에게 그것을 알아낼 못할 문제가 될 수
import { async, ComponentFixture, TestBed, getTestBed, inject } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { HttpPostService } from './http-post.service';
import {
BaseRequestOptions, Http, XHRBackend, HttpModule,
Response, ResponseOptions, RequestMethod
} from '@angular/http';
import { ICourseModel } from '../interface/course-model';
describe('HttpPostService',() => {
let mockBackend: MockBackend;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
HttpPostService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
deps: [MockBackend, BaseRequestOptions],
useFactory:
(backend: XHRBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}
}
],
imports: [
HttpModule
]
});
mockBackend = getTestBed().get(MockBackend);
}));
it('should insert new courseList',
async(inject([HttpPostService], (service: HttpPostService) => {
mockBackend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Post);
connection.mockRespond(new Response(new ResponseOptions({})));
const contentType = connection.request.headers.get('Content-Type');
expect(contentType).not.toBeNull();
expect(contentType).toEqual('application/json');
expect(connection.request.url).toBe('someurl/data.json');
});
const courseList: ICourseModel[] = [
{ 'course': 'Mobile Development' },
{ 'course': 'Web Development' },
{ 'course': 'IOS Development' },
{ 'course': 'Android Development' }
];
const result = service.storeData(courseList);
result.subscribe(
(successResult) => {
expect(successResult).toBeDefined();
expect(successResult).toEqual({});
});
})));
});
// HTTP 포스트 서비스
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { ICourseModel } from '../interface/course-model';
import { Observable } from 'rxjs/Observable';
// HTTP Post service to post the data to the server. Add Content-type in the header while posting a data
@Injectable()
export class HttpPostService {
constructor(private http: Http) { }
storeData(crs: ICourseModel[]) {
const headers = new Headers({'Content-Type': 'application/json'});
headers.append('Accept', 'application/json');
return this.http.post('someurl/data.json', crs, {headers: headers}).catch(
(error: Response) => {
return Observable.throw(error);
}
);
}
}
나는 이것을 원하지 않는다. 그러나 이것은 나의 과제이다. 그래서 나는해야한다. 제 코드로 대답 해 주시겠습니까? 덕분에 도와 주셔서 감사합니다 – SONGSTER
그럼 공식 테스트와 그 설정을 보도록 제안합니다 : https://github.com/angular/angular/blob/master/packages/http/test/http_spec.ts – Sasxa
한 번 더 질문, 테스트에서 응답의 'Content-type'을 어떻게 확인합니까? – SONGSTER