2017-03-29 7 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宣言の中にexpectを追加してください。

例:

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

関連する問題