我試圖在Angular 4中測試一個HttpInterceptor。我發現當我打電話給HttpClient.get()時,它發生錯誤什麼時候Angular4 HttpClient.get()返回undefined?
TypeError:您提供了'undefined',其中一個流是預期的。您可以提供Observable,Promise,Array或Iterable。
http.get('/ data')何時返回undefined?
import { Injectable } from '@angular/core';
import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClient, HttpClientModule, HttpHandler } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import 'rxjs/add/operator/toPromise';
describe('AppComponent',() => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, HttpClientModule],
providers: [ HttpClient, HttpHandler,
// {
// provide: HTTP_INTERCEPTORS,
// useClass: AuthErrorHttpInterceptorService,
// multi: true
// }
]
}).compileComponents();
}));
it('adding header test', inject([HttpClient], (http: HttpClient) => {
debugger;
const httpMock = TestBed.get(HttpTestingController);
// Make an HTTP GET request, and expect that it return an object
// of the form {name: 'Test Data'}.
http.get('/data')
.subscribe(data => expect(data['name']).toEqual('Test Data'));
// At this point, the request is pending, and no response has been
// sent. The next step is to expect that the request happened.
const req = httpMock.expectOne('/data');
// If no request with that URL was made, or if multiple requests match,
// expectOne() would throw. However this test makes only one request to
// this URL, so it will match and return a mock request. The mock request
// can be used to deliver a response or make assertions against the
// request. In this case, the test asserts that the request is a GET.
expect(req.request.method).toEqual('GET');
// Next, fulfill the request by transmitting a response.
req.flush({name: 'Test Data'});
// Finally, assert that there are no outstanding requests.
httpMock.verify();
}));
});
類型錯誤:其中一個流,預計您提供 '未定義'。您可以提供Observable,Promise,Array或Iterable。
編輯:我見過問題Error while unit testing HttpClientModule (Angular 4.3+)和導入的HttpClientModule,但我仍然得到錯誤。此外,我不包括我的攔截器組件,直到我解決這個錯誤,所以有問題不能在那裏。
編輯:const req = httpMock.expectOne('/data');
這一行從不執行,因爲錯誤被從線拋出上述
編輯:解決方案是從所述提供者陣列
你採用t他是HTTP測試模塊,但您從未實際選擇請求或刷新響應。閱讀相關文檔:https://angular.io/guide/http#testing-http-requests – jonrsharpe
好的謝謝,這可能是爲什麼我得到錯誤。讓我檢查 –
嗨jonrsharpe,我編輯了問題的代碼從文檔,但我仍然得到錯誤,你提供'未定義'在哪裏預期流 –