简体   繁体   English

规格没有期望Angular 7

[英]Spec has no expectations Angular 7

I am using Jasmine & Karma for unit testing angular app . 我正在使用Jasmine&Karma进行角度应用程序的单元测试。 I have wrote unit tests like this: 我写了这样的单元测试:

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');}
        )
      }));
})

When I execute test cases , I see a message SPEC HAS NO EXPECTATIONS for both test cases . 当我执行测试用例时,我看到一条消息“对于两个测试用例都没有期望值”。 I am wondering why it shows spec has no expectations. 我想知道为什么它表明规格没有期望。

Then I followed the suggested solutions in this post: Spec has no expectations - Jasmine testing the callback function 然后,我遵循了本文中建议的解决方案: Spec没什么期望-Jasmine测试回调函数

Using done() : 使用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();}
        )
      }));
})

Again, Jasmine tells me that the spec has no expectations 再次,茉莉花告诉我, spec has no expectations

Awaiting a subscription doesn't do anything... 等待订阅没有任何作用...

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

You need to convert the observable to a Promise. 您需要将可观察对象转换为Promise。 Also, make sure your observable completes, otherwise you need to get only the first element or the promise will never resolve (let's say that getAll continues to send events or something - but you should get a timeout during the running of the tests). 另外,请确保您的可观察性完成,否则您只需要获取第一个元素,否则promise将永远无法解决(假设getAll继续发送事件或其他内容-但您应该在测试运行期间超时)。 This should do the trick: 这应该可以解决问题:

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');
        }
    }));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM