【问题标题】:Spec has no expectations Angular 7Spec对Angular 7没有期望
【发布时间】:2019-07-22 00:38:03
【问题描述】:

我正在使用 Jasmine 和 Karma 对 angular app 进行单元测试。我写过这样的单元测试:

describe('#getAll', async () => {
    it('#should return user intergration_tester', inject([UsersService], async(service: UsersService) => {
        await service.getAll('integration_tester', access_token).subscribe(
            user => {
                expect(user[0].firstName).toContain('Integration');
                done();
            })
      }));

      it('#should return error 404', inject([UsersService], (service: UsersService) => {
        service.getAll('integration_tester', '').subscribe(
            user => {expect(user[0].firstName).not.toContain('Integration');},
            err => { expect(err).toContain('error');}
        )
      }));
})

当我执行测试用例时,我看到一条消息 SPEC HAS NO EXPECTATIONS for both test cases。我想知道为什么它显示规范没有期望。

然后我按照这篇文章中建议的解决方案:Spec has no expectations - Jasmine testing the callback function

使用done()

describe('#getAll', async () => {
    it('#should return user intergration_tester', inject([UsersService], async(service: UsersService, done) => {
        await service.getAll('integration_tester', access_token).subscribe(
            user => {
                expect(user[0].firstName).toContain('Integration');
                done();
            })
      }));

      it('#should return error 404', inject([UsersService], (service: UsersService, done) => {
        service.getAll('integration_tester', '').subscribe(
            user => {expect(user[0].firstName).not.toContain('Integration'); done();},
            err => { expect(err).toContain('error'); done();}
        )
      }));
})

Jasmine 再次告诉我spec has no expectations

【问题讨论】:

标签: angular unit-testing jasmine karma-jasmine


【解决方案1】:

等待订阅没有任何作用...

await service.getAll(...).subscribe(...)

您需要将 observable 转换为 Promise。另外,请确保您的 observable 完成,否则您只需要获取第一个元素,否则承诺将永远无法解决(假设 getAll 继续发送事件或其他东西 - 但您应该在测试运行期间获得超时)。这应该可以解决问题:

describe('#getAll', async () => {
    it('#should return user intergration_tester', inject([UsersService], async (service: UsersService) => {
        const user = await service.getAll('integration_tester', access_token).toPromise();
        expect(user[0].firstName).toContain('Integration');
    }));

    it('#should return error 404', inject([UsersService], async (service: UsersService) => {
        try {
            const user = await service.getAll('integration_tester', '').toPromise();

            expect(user[0].firstName).not.toContain('Integration');
        } catch (err) {
            // why do you also expect an error to be thrown?
            expect(err).toContain('error');
        }
    }));
})

【讨论】:

  • 感谢您的帮助。现在我收到以下错误消息:错误:超时 - 未调用异步回调。该服务向真实服务器发送请求。您认为问题与此有关吗?
  • service.getAll 是否返回一个可观察的事件?意味着它发出一个事件并完成(如 HttpClient 服务)?如果是这样,您需要检查实际请求需要多少。我认为默认超时是 2000 毫秒。如果您的请求花费的时间超过此时间,则测试运行程序将失败,因此您需要增加超时时间。另外,检查inject 是否真的调用了您的代码(添加console.log 或其他内容)。
  • 感谢您的帮助。我导入了 HttpClientTestingModule 而不是 HttpClientModule。那是我的错误。
  • @AndreiTătar 您的解决方案有效,但我仍然收到警告“SPECT HAS NO EXPECTATIONS”?没有任何错误,并且确定“期望”所在的行已被编译器覆盖。
猜你喜欢
  • 2017-06-28
  • 2019-08-01
  • 2016-10-22
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多