[英]TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable
[英]Unit testing - getting TypeError: You provided an invalid object where a stream was expected
我正在嘗試進行單元測試,其中效果使用服務從 API 獲取結果,然后觸發成功操作或錯誤操作。 我不斷收到錯誤TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
. 不完全確定需要更改什么才能使其正常工作。
包括錯誤檢查測試和相關代碼。
角
Angular CLI: 7.1.4
Node: 10.5.0
OS: win32 x64
Angular: 7.1.4
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.10.7
@angular-devkit/build-angular 0.10.7
@angular-devkit/build-optimizer 0.10.7
@angular-devkit/build-webpack 0.10.7
@angular-devkit/core 7.0.7
@angular-devkit/schematics 7.1.4
@angular/cdk 7.2.0
@angular/material 7.2.0
@ngtools/webpack 7.0.7
@schematics/angular 7.1.4
@schematics/update 0.11.4
rxjs 6.3.3
typescript 3.1.6
webpack 4.19.1
測試
describe('ResultsEffects', () => {
let store: jasmine.SpyObj<Store<FeatureState>>;
let ebs: jasmine.SpyObj<EBSService>;
let snackbar: jasmine.SpyObj<SnackBarService>;
beforeEach(() => {
store = jasmine.createSpyObj('store', ['pipe']);
ebs = jasmine.createSpyObj('EBSService', ['getPollResults', 'getUserHasVoted', 'getSettings']);
snackbar = jasmine.createSpyObj('SnackBarService', ['notify']);
});
describe('fetchResults', () => {
it(`should dispatch 'FetchPanelResultsError'`, () => {
const fetchAction = new FetchPanelResults();
const error = { statusText: 'ERROR' };
const errorAction = new FetchPanelResultsError({ error: error.statusText });
const values = {
a: fetchAction,
e: errorAction
};
const source = cold('a', values);
const expected = cold('--e', values);
const actions = new Actions(source);
ebs.getPollResults.and.returnValue(throwError(error));
const effects = new ResultsEffects(
actions,
store,
snackbar,
ebs
);
expect(
effects.fetchResults$({
debounce: 20,
scheduler: getTestScheduler()
})
).toBeObservable(expected);
});
});
});
影響
@Effect()
fetchResults$ = ({ debounce = 500, scheduler = asyncScheduler } = {}) => this.actions$.pipe(
ofType<SetPanelResults>(PanelResultsActionTypes.FETCH_PANEL_RESULTS),
// need alternate method for this
// withLatestFrom(this.featureState$),
// map(([_, state]) => state.twitch.token),
debounceTime(debounce, scheduler),
switchMap((token) =>
this.ebs.getPollResults(token).pipe(
map(res => new SetPanelResults(res.data.results)),
catchError(error => of(new FetchPanelResultsError({ error: error.statusText })))
)
)
)
服務
getPollResults(token: string): Observable<PollResultsFetchData> {
const options = {
headers: ebsHeaders(token)
};
return this.http.get<PollResultsFetchData>(`${this.ebsURL}/poll/results`, options);
}
更新:添加去抖動以觸發彈珠錯誤。 仍然需要替代withLatestFrom
。
根據評論:
問題是withLatest
from 並沒有真正返回任何東西,因為您沒有功能狀態。 這是因為您使用jasmine.createSpyObj('store', ['pipe'])
模擬了商店。
要解決這個問題,請使用“真實”的 ngrx 存儲或使用在 NgRx 7 中實現的模擬存儲。
對我來說,在開發過程中,Live Reload 上的錯誤開始定期發生幾個月(Angular 7)。 我只是通過暫停幾秒鍾重新打開瀏覽器來修復它。
今天重新打開並沒有幫助,我偶爾會發現,重新保存源文件之一(即使沒有實際更改它)會有所幫助並且錯誤消失。 重新保存通過 webpack 和實時重新加載觸發項目內存構建重新編譯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.