Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Spec没有期望值7_Angular_Unit Testing_Jasmine_Karma Jasmine - Fatal编程技术网

Angular Spec没有期望值7

Angular Spec没有期望值7,angular,unit-testing,jasmine,karma-jasmine,Angular,Unit Testing,Jasmine,Karma Jasmine,我正在使用Jasmine&Karma进行单元测试。我写过如下的单元测试: describe('#getAll', async () => { it('#should return user intergration_tester', inject([UsersService], async(service: UsersService) => { await service.getAll('integration_tester', access_token).s

我正在使用Jasmine&Karma进行单元测试。我写过如下的单元测试:

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没有期望

然后,我在这篇文章中遵循了建议的解决方案:

使用
done()


Jasmine再次告诉我,
规范没有任何期望

等待订阅没有任何作用

wait service.getAll(…).subscribe(…)

你需要将可观察的转化为承诺。另外,请确保您的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没有期望”?没有任何错误,并且确信编译器已经检查了“Expect”所在的行。
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');
        }
    }));
})