【问题标题】:TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at <Jasmine>TypeError:您在预期流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。在 <茉莉花>
【发布时间】:2023-03-13 02:43:01
【问题描述】:

您好,我在 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):执行 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:您在预期流的位置提供了“未定义”。您可以提供 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/repeatWhen.js:11:1) 在 Observable.subscribe (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/Observable.js:23:1) 在 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/karma_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) 失败:无法读取未定义的属性“取消订阅” 在 在 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)

【问题讨论】:

    标签: angular unit-testing jasmine karma-jasmine


    【解决方案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));
    

    【讨论】:

      【解决方案2】:
      1. 首先使用 xdescribe 禁用所有测试

      2. 一一启用,直到发现有问题的单元测试 它应该是使用模拟方法并返回 observable 而不是 value 的提供者,例如

        { 提供: TranslateService, useValue: { get: () => of(''), 即时: () => of('') } }, { 提供:SomeService,useValue:{ getInfos: () => of() ,getList: () => [] },

      • instant 应该返回值,不可观察
      • getInfos 应该返回一些可观察的结果,而不是未定义的
      • getList 应该返回可观察的数组,而不是具体的空数组...

      对我来说,这些案例中的任何一个都确实导致了不一致的真阴性和假阳性......

      1. 当您检测到它时,修复提供程序并启用其余测试。

      祝你好运,这是一个卑鄙的人......

      【讨论】:

        【解决方案3】:

        我也有类似的问题。

        我建议在您的 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));
        

        【讨论】:

          猜你喜欢
          • 2021-12-10
          • 2018-09-07
          • 2017-09-18
          • 2020-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-28
          相关资源
          最近更新 更多