2017-04-02 3 views
0

http 게시 메소드가 포함 된 주입 된 서비스로 구성 요소를 테스트하려고합니다.카르마 테스트 주입 서비스

형식 오류가 :이 저장 방법입니다

: 정의되지 않은

의 특성 '응답'을 읽을 수 없습니다 것은이 내 코드입니다 내가 실행할 때하지만 테스트 카르마 나 아래의 메시지를 보여줍니다 SignupService ADDUSER 메소드 호출 버튼 ADDUSER 메소드 호출 :

save() { 
this.addUser(); 
this.onShowModal(); 


} 

    private addUser(){ 
    // Copy the form values over the product object values 
    let user = Object.assign({}, this.customer, this.signUpForm.value); 
    this.signUpService.addUser(user).subscribe((result) =>   this.onSaveComplete(result), 
    (error: any) => this.errorMessage = <any>error); 
    this.modalMessage = this.errorMessage 


} 

이는 t이고 그는 SignupService ADDUSER 방법 :

it('should save the user submetted',() => { 
    component.save(); 
    let signup = component.signUpForm; 
    signup.get('firstName').setValue('BEN'); 
    signup.get('secondName').setValue('wis'); 
    signup.get('username').setValue('wiss013'); 
    signup.get('email').setValue('[email protected]'); 
    signup.get('passwordMatch').get('password').setValue('wis'); 
    signup.get('passwordMatch').get('confirmPassword').setValue('wis'); 
    let show = component.showModal; 
    expect(show).toBeTruthy(); 
}); 
}); 

중 하나가 제발 도움이 될 수 있습니다

addUser (user : Customer){ 
let headers = new Headers({ 'Content-Type': 'application/json' }); 
let options = new RequestOptions({ headers: headers }); 
return this.http.post(this.url,user,options) 
    .map(this.extractResponseData) 
    .do(data => console.log('Add user : ' + JSON.stringify(data))) 
    .catch(this.handleError); 

private handleError(error: Response) { 
return Observable.throw(error.json().error || 'Server error');} 



private extractResponseData(response: Response){ 
let body = response.json(); 
console.log(body.data); 
return body.data || {}; 

}

을하고 마지막으로이 오류를 처리하는 내 사양이다?!

+0

구성 요소를 테스트하는 경우 서비스를 조롱하십시오. 서비스를 테스트하는 경우 HTTP 요청을 모의 해보십시오. 한 번에 하나의 장치 만 테스트해야합니다. – estus

+0

답장을 보내 주셔서 감사합니다.하지만 여전히 작동하지 않고 동일한 오류가 발생합니다. 내 사양이 ExtractResponseData 함수의 응답 모듈을 인식하지 못하는 것 같습니다. 이러한 사양 파일의 임포트 모듈이다'beforeEach (비동기 (() => { TestBed.configureTestingModule ({ 선언 [SignUpComponent] 수입 [FormsModule, BrowserModule, ReactiveFormsModule, ModalModule.forRoot() HttpModule], 공급자 : [SignUpService, Http, ConnectionBackend] } .compileComponents(); })))); ' –

답변

0
describe('Test you service',() => { 
    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     providers: [YOUR_SERVICE] 
    }); 
    }); 

    it('hashCode should return correct hash code', inject([YOUR_SERVICE], (service: YOUR_SERVICE) => { 
    .... 
    })); 
});