答えて

0

あなたはリファレンスMockBackend を使用して、HTTP応答を模擬することができますhttps://angular.io/api/http/testing/MockBackend

あなたは簡単に独自の応答やテストを定義することができ、すべての場合/他

// before each test 
beforeEach(() => { 
    this.injector = ReflectiveInjector.resolveAndCreate([ 
     {provide: ConnectionBackend, useClass: MockBackend}, 
     {provide: RequestOptions, useClass: BaseRequestOptions}, 
     Http, 
     // other dependencies like Router/AppConfig 
    ]); 
    this.authenticationService = this.injector.get(AuthenticationService); 
    this.backend = this.injector.get(ConnectionBackend) as MockBackend; 
    this.backend.connections.subscribe((connection: any) => this.lastConnection = connection); 
}); 


// in your test 
const mockErrorResponse = {status: 200}; 
this.lastConnection.mockRespond(new Response(new ResponseOptions({body: JSON.stringify(mockErrorResponse)}))); 
tick(); 
関連する問題