Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Prueba de integración de servicios observables de Angular 2

Me gustaría crear una prueba de integración y acceder a mi servicio real (no un simulacro). ¿Cómo haría eso en Angular 2?

Esto es lo que tengo para mi servicio observable:

 import { Injectable } from '@angular/core'; import { Http, Response, RequestOptions, Headers } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import 'rxjs/add/observable/throw'; import { UserDetail } from '../models/user-detail.model'; export class GetUserDetailService { private _userDetailUrl : string = 'http://ourserver/api/GetCurrentUserDetail'; constructor(private _http: Http) { } getUserDetail(): Observable<UserDetail> { return this._http.get(this._userDetailUrl) .map((response: Response) => <UserDetail> response.json()) .do(data => console.log('All: ' + JSON.stringify(data))) .catch(this.handleError); } private handleError(error: Response) { console.error(error); return Observable.throw(error.json().error || 'Server error'); } }

¿Cómo me aseguro de que un objeto sea devuelto por el servicio?

 // Third party imports import { Observable } from 'rxjs/Observable'; import { HttpModule } from '@angular/http'; // Our imports import { GetUserDetailService } from './get-user-detail.service'; import { UserDetail } from '../models/user-detail.model'; describe('GetUserDetailService', () => { beforeEach(() => { // What do I put here? }); it('should get the user detail', () => { // What do I put here? }); });
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como se muestra en este ejemplo , una forma de realizar pruebas de integración XHR en Angular es simplemente usar HttpModule sin MockBackend :

 describe('GetUserDetailService', () => { beforeEach(() => { TestBed.configureTestingModule({ provide: [GetUserDetailService], imports: [HttpModule] }); }); it('should get user detail', async(inject([Http, GetUserDetailService], (http, svc) => { spyOn(http, 'get').and.callThrough(); svc.getUserDetail().subscribe( (userDetail: UserDetail) => { expect(userDetail).toEqual(...); } ); expect(http.get).toHaveBeenCalledWith(...); }))); it('shouldnot get user detail', async(inject([Http, GetUserDetailService], (http, svc) => { spyOn(http, 'get').and.callThrough(); spyOn(svc, 'handleError').and.callThrough(); svc._userDetailUrl = 'wrong url'; svc.getUserDetail().subscribe( (userDetail: UserDetail) => { throw new Error('should fail') }, (error) => { expect(svc.handleError).toHaveBeenCalledWith(...); expect(error).toEqual(...); } ); expect(http.get).toHaveBeenCalledWith('wrong url'); }))); });

El ayudante fakeAsync no se puede usar en una prueba realmente asíncrona (debería haber un error si se usa), por lo que es async .

La primera prueba falla automáticamente si hubo un error en observable, por lo que no tiene que detectarse en subscribe .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!