角度可观察测试以确保调用了订阅

时间:2019-03-13 17:13:02

标签: angular jasmine observable

我是Angular的新手,我对Jasmine Observables单元测试有疑问。具体来说,我想进行负面测试,以确保调用了可观察对象的订阅方法,否则该测试会给出错误的肯定结果。

我有一个基本的组件,可以显示报告名称和链接的列表。 reports属性在component.ts文件中声明如下:

reports$: Observable<Report[]>;

在“ beforeEach”块中,我设置了3个测试报告的列表,并分配给reports $属性。

此测试未能按预期进行:

 it('correctly fails because the test uses async.', async(() => {

    component.reports$.subscribe(val => {
      expect(val.length).toEqual(3);

      expect(1).toEqual(2); // this does fail as it should
    });

  }));

此测试通过,但显然是假阳性。我可以在测试中包含任何内容以确保测试失败吗?当向项目中添加其他单元测试时,我正在考虑更大的前景。异步包装器是不是要走的路?

  it('passes but its a false positive because the async is not used by the test', () => {

        component.reports$.subscribe(val => {
          expect(val.length).toEqual(3);

          expect(1).toEqual(2); // obviously should fail
        });

        // what can I do in this test to ensure the subscribe was called?

      });

0 个答案:

没有答案