[英]Error: "You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable." in my Auth Interceptor
[英]TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at <Jasmine>
嗨,我已經在 ngonit 中添加了這段代碼,用於輪詢,添加后我所有的測試用例都失敗了。 請幫忙怎么解決?
ngOnInit(): void {
this.timeInterval = interval(5000)
.pipe(
startWith(0),
switchMap(() => this.deviceService.getDeviceList())
)
.pipe(
takeUntil(this.deviceService.flag$),
repeatWhen(() => this.deviceService.flag$)
)
.subscribe((success: any) => {
this.gridApi?.setRowData(this.updatedData);
console.log("hello")
this.updatedData = success;
}, retry(2));
這是完整的錯誤
HeadlessChrome 88.0.4324 (Windows 10.0.0) DeviceComponent testing columnresize of the grid for other scenario FAILED
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at <Jasmine>
at subscribeTo (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/util/subscribeTo.js:27:1)
at innerSubscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/innerSubscribe.js:69:23)
at TakeUntilOperator.call (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/operators/takeUntil.js:11:52)
at Observable.subscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Observable.js:23:1)
at RepeatWhenOperator.call (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/operators/repeatWhen.js:11:1)
at Observable.subscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Observable.js:23:1)
at DeviceComponent.ngOnInit (http://localhost:9876/_karma_webpack_/src/app/device/device.component.ts:99:4)
at callHook (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:3937:1)
at callHooks (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:3901:1)
at executeInitAndCheckHooks (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:3842:1)
Failed: Cannot read property 'unsubscribe' of undefined
at <Jasmine>
at DeviceComponent.ngOnDestroy (http://localhost:9876/_karma_webpack_/main.js:28373:23)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/device/device.component.spec.ts:196:15)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
at AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:763:1)
at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:302:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
at Zone.runGuarded (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:134:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:885:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:823:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
HeadlessChrome 88.0.4324 (Windows 10.0.0): Executed 1 of 385 (1 FAILED) (0 secs / 1.732 secs) HeadlessChrome 88.0.4324 (Windows 10.0.0) DeviceComponent testing columnresize of the grid for other scenario FAILED TypeError: You provide '未定義',其中預期為 stream。 您可以提供 Observable、Promise、Array 或 Iterable。 在 subscribeTo (http://localhost:9876/ karma_webpack /node_modules/rxjs/_esm2015/internal/util/subscribeTo.js:27:1) 在innerSubscribe (http://localhost:9876/ karma_webpack /node_modules/rxjs/_esm2015 /internal/innerSubscribe.js:69:23) 在 TakeUntilOperator.call (http://localhost:9876/ karma_webpack /node_modules/rxjs/_esm2015/internal/operators/takeUntil.js:11:52) 在 Observable.subscribe (http ://localhost:9876/karma_webpack/ node_modules /rxjs/_esm2015/internal/Observable.js:23:1) 在RepeatWhenOperator.call (http://localhost:9876/ karma_webpack /node_modules/rxjs/_esm2015/internal/operators/在DeviceComponent.ngOnInit (http://localhost: 9876/ karma_webpack /src/app/device/device.component.ts:99:4) 在 callHook (http://localhost:9876/ karma_webpack /node_modules/@angular/core/ ivy_ngcc /fesm2015/core.js:3937: 1) 在 callHooks (http://localhost:9876/ kar ma_webpack /node_modules/@angular/core/ ivy_ngcc /fesm2015/core.js:3901:1) 在 executeInitAndCheckHooks (http://localhost:9876/ karma_webpack /node_modules/@angular/core/ ivy_ngcc /fesm2015/core.js:3842 :1) 失敗:無法在 UserContext 的 DeviceComponent.ngOnDestroy (http://localhost:9876/ karma_webpack /main.js:28373:23) 處讀取未定義的屬性“取消訂閱”。 (http://localhost:9876/ karma_webpack /src/app/device/device.component.spec.ts:196:15) 在 ZoneDelegate.invoke (http://localhost:9876/ karma_webpack /node_modules/zone.js/ dist/zone-evergreen.js:365:1)
試試這個來調試它:
ngOnInit(): void {
console.log('this.deviceService.getDeviceList()', this.deviceService.getDeviceList());
console.log('this.deviceService.flag$', this.device.getDeviceList());
// make sure the above logs are not defined.
// If they are, you're not mocking them correctly when `ngOnInit` is called.
// `ngOnInit` is called after the first `fixture.detectChanges()` after
// the `TestBed.configureTestingModule({}).compileComponents();`.
this.timeInterval = interval(5000)
.pipe(
startWith(0),
switchMap(() => this.deviceService.getDeviceList())
)
.pipe(
takeUntil(this.deviceService.flag$),
repeatWhen(() => this.deviceService.flag$)
)
.subscribe((success: any) => {
this.gridApi?.setRowData(this.updatedData);
console.log("hello")
this.updatedData = success;
}, retry(2));
首先使用 xdescribe 禁用所有測試
一個個啟用,直到發現有問題的 unittest 應該是使用提供者模擬方法並返回 observable 而不是 value 的提供者,例如
{提供:TranslateService,useValue:{獲取:() => of(''),即時:() => of('')}},{提供:SomeService,useValue:{getInfos:() => of( ), getList: () => [] },
對我來說,這些案例中的任何一個都確實導致了不一致的真陰性和假陽性......
祝你好運,這是一個卑鄙的人......
我有一個類似的問題。
我建議在您的 2 個 switchMap 運算符調用中檢查這些方法是否返回 Observables:
this.deviceService.getDeviceList()
this.deviceService.flag$
這就是我的原因。 switchMap 操作符必須返回一個 Observable。 因此,您的代碼中可能沒有訂閱 Observable 的情況發生。
如果您不需要在這些方法中返回 Observables,請嘗試使用 of(),如下所示:
// DO NOT forget to import 'of' from rxjs :)
ngOnInit(): void {
this.timeInterval = interval(5000)
.pipe(
startWith(0),
switchMap(() => of(this.deviceService.getDeviceList()))
)
.pipe(
takeUntil(this.deviceService.flag$),
repeatWhen(() => of(this.deviceService.flag$))
)
.subscribe((success: any) => {
this.gridApi?.setRowData(this.updatedData);
console.log("hello")
this.updatedData = success;
}, retry(2));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.