2017-03-29 2 views
2

Http 서비스에서 올바른 엔드 포인트를 호출하는지 확인하고 싶습니다. 그렇게 할 수 있습니까?Angular2 Jasmine spyOn http calls

지금까지는 내 서비스에서 테스트 한 것은 데이터를 다시 보내는 것입니다 ... 더 유용하게 만들고 싶습니다. 거의 다

import { TestBed, async, inject } from '@angular/core/testing'; 
import { SignupService } from './signup.service'; 
import { Observable } from 'rxjs/Observable'; 

import { HttpModule, Http, Response, ResponseOptions, RequestOptions, Headers, XHRBackend } from '@angular/http'; 
import { MockBackend, MockConnection } from '@angular/http/testing'; 
import { ErrorHandlerService } from '../../services/error-handler.service'; 

describe('SignupService',() => { 

    let errorStub = {}; 

    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     imports: [HttpModule], 
     providers: [ 
     SignupService, 
     { provide: XHRBackend, useClass: MockBackend }, 
     { provide: ErrorHandlerService, useValue: errorStub } 
     ] 
    }); 
    }); 

    it('signup should return an application error if no apps are provided', inject([SignupService], (service: SignupService) => { 
    service.signup('', [], null).subscribe(
     suc => { }, 
     err => expect(err).toMatch(/application/) 
    ); 
    })); 

    it('signup should return a role error if no roles are provided', inject([SignupService], (service: SignupService) => { 
    service.signup('', null, []).subscribe(
     suc => { }, 
     err => expect(err).toMatch(/rôle/) 
    ); 
    })); 

    it(
    'signup should return a response with a "contenu" parameter', 
    inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => { 
     let content = 'anything'; 
     mockBackEnd.connections.subscribe((connection: MockConnection) => { 
     connection.mockRespond(new Response(new ResponseOptions({ 
      body: JSON.stringify({ 
      contenu: content 
      }) 
     }))); 
     }); 
     service.signup('', [], []).subscribe(
     suc => expect(suc).toBe(content) 
    ); 
    })); 

    it(
    'checkUser should return a response with a "contenu" parameter', 
    inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => { 
     let content = 'anything'; 
     mockBackEnd.connections.subscribe((connection: MockConnection) => { 
     connection.mockRespond(new Response(new ResponseOptions({ 
      body: JSON.stringify({ 
      contenu: content 
      }) 
     }))); 
     }); 
     service.checkUser('').subscribe(
     suc => expect(suc).toBe(content) 
    ); 
    })); 
}); 

답변

2

: 만약 당신이 그것을 필요로하는 경우

, 여기에 내 현재 사양 파일입니다! 그냥 mockBackend 선언 안에 기대를 추가하십시오.

예 :

mockBackend.connections.subscribe(connection => { 

     // Check if it invokes a http POST call 
     expect(connection.request.method).toBe(RequestMethod.Post); 

     // Check if the URL is correct 
     expect(connection.request.url).toBe('/some-url'); 

     connection.mockRespond(new Response(new ResponseOptions({ 
     body: JSON.stringify(''), 
     status: 200 
     }))); 

    }); 
+0

와우, 나는 직장에서 떨어져 일주일에 가서 완전히 (내가 포기하고 그것을 테스트하지 않기로 결정했다) 해당 게시물에 대해 잊어 버렸습니다. 귀하의 솔루션이 작동합니다. 고마워 친구 – trichetriche