Angular 2单元测试“无法读取未定义的属性'unsubscribe'”

施托普杰夫

我有带有ngOnInit和ngOnDestroy方法以及ActivatedRoute订阅的示例TestComp。

@Component({
    selector: 'test',
    template: '',
})
export class TestComp implements OnInit, OnDestroy {

    constructor (
        private route: ActivatedRoute
    ) {
    }

    ngOnInit() {
        this.subscription = this.route.data
            .subscribe(data => {
                console.log('data', data.name);
            })
        ;
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }
}

从规范文件调用ngOnDestroy方法时(或在运行多个测试时),出现“无法读取未定义的属性'unsubscribe'”。

我的规格文件:

describe('TestComp', () => {
    let comp: TestComp;
  let fixture: ComponentFixture<TestComp>;

  beforeEach(async(() => {
    TestBed
    .configureTestingModule({
      declarations: [TestComp],
      imports: [RouterTestingModule],
      providers: [
        {
        provide: ActivatedRoute,
        useValue: {
          data: {
            subscribe: (fn: (value: Data) => void) => fn({
              name: 'Stepan'
            })
          }
        }
      }
        // { //Also tried this
        //   provide: ActivatedRoute,
        //   useValue: {
        //     params: Observable.of({name: 'Stepan'})
        //   }
        // }
      ]
    })
    .compileComponents()
    .then(() => {
          fixture = TestBed.createComponent(TestComp);        
          fixture.detectChanges();
    })
    }));

  it('TestComp successfully initialized', () => {
    fixture.componentInstance.ngOnInit();
    expect(fixture.componentInstance).toBeDefined()
    fixture.componentInstance.ngOnDestroy();
  });
});

我根据此处的答案传递了ActivatedRoute值,但出现错误。所以我的问题是-我应该通过什么作为ActivatedRoute来进行订阅和退订?示例柱塞

瓦迪姆

模拟服务时只需使用观察器即可:

{
    provide: ActivatedRoute,
    useValue: { data: Observable.of({ name: 'Stepan' }) }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular2:无法读取未定义的属性“名称”

运行npm测试(Angular 2单元测试)后无法读取未定义的属性“ subscribe”

Angular 2单元测试-@ViewChild未定义

无法读取未定义的属性退订:Angular2测试

Angular2无法读取未定义的属性“ push”

无法读取未定义[Angular2]的属性“ isStopped”

Angular 2单元测试-无法读取未定义的属性“ root”

Angular 2 TypeError:无法读取未定义的属性“ toUpperCase”

无法读取未定义angular2的属性

Angular 2单元测试错误无法获取未定义或空引用的属性“ preventDefault”

Angular 4单元测试错误“ TypeError:无法读取未定义的属性'subscribe'”

TypeError:在angular2中执行单元测试时,无法读取未定义故障的属性“快照”

Angular 2+无法读取未定义的属性“ id”

Angular2-单元测试可观察到的错误“无法读取未定义的属性'subscribe'”

angular2单元测试:无法读取componentInstance.method()的属性未定义

无法读取未定义的属性“ post”-Angular 2

无法读取Angular 2中未定义的属性“错误”?

Angular 2`无法读取未定义的属性'dataService'

无法读取未定义的属性“ unsubscribe”(rxJS-订阅)

Angular - Ionic 2 - 无法读取未定义的属性“订阅”

单元测试 Angular 4 出现错误“TypeError:无法读取未定义的属性“订阅”

无法读取未定义的属性“登录” - 测试间谍 - Angular2+

类型错误:在单元测试 Angular 6 时无法读取未定义的属性“查询”

Angular 单元测试为 Http Post 返回“无法读取未定义的属性”

对具有路由器依赖项的 Angular 服务进行单元测试:“TypeError:无法读取未定义的属性‘导航’”

无法设置未定义的属性“http” - Angular 单元测试

Angular 8:ActivatedRoute 上的 Jasmine 单元测试,TypeError:无法读取未定义的属性“父”

Angular 单元测试 - TypeError:无法读取未定义的属性“名称”

Angular 单元测试自定义验证器无法读取未定义的属性(读取“dobLengthValidator”)