使用Jasmine toThrowError时似乎无法捕获错误 [英] Can't seem to catch error when using Jasmine toThrowError

查看:231
本文介绍了使用Jasmine toThrowError时似乎无法捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始尝试jasmine并且我想使用toThrowError()函数但我的测试不想成功。

I beginning to try jasmine and I want to use the toThrowError() function but my test does not want to succeed.

我有一个函数我扔了错误:

I have one function where I throw an error:

test.service.ts

test(list:{}){ 
    if(list == null){
        throw new TypeError();
    }
    else{
        // do something...
    }
}

我的测试:

it('shall throw an error', inject()
    [
        TestService
    ],
    (
        testService: TestService
    ) => {
        let test = testService.test(null);
        expect(test).toThrowError(TypeError);
    }
);

我的测试失败并显示未捕获的TypeError(当我调用它时我正在尝试赶紧)。

And my test fails with an Uncaught TypeError (when I call it I'm doing it in a try catch).

推荐答案

你应该期待一个用 null调用你的函数的函数,抛出错误。现在你期望调用函数的结果抛出一个错误,这是没有发生的。

You should expect a function, that calls your function with null, to throw an error. Right now you're expecting the result of calling the function to throw an error, which isn't happening.

expect(() => testService.test(null)).toThrowError(TypeError);

这篇关于使用Jasmine toThrowError时似乎无法捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆