如何测试AngularJS中是否抛出异常

时间:2022-07-14 20:28:41

I need to test a directive, and it should throw an exception. How can I test that the exception was thrown, in jasmine?

我需要测试一个指令,它应该抛出异常。如何在茉莉花中测试抛出异常?

The directives link function:

指令链接功能:

link: function() {
    if(something) {
        throw new TypeError('Error message');
    }
}

I have not yet successfully implemented a test that actually catches the error and reports that the test was successful.

我还没有成功实现一个实际捕获错误并报告测试成功的测试。

4 个解决方案

#1


20  

This is how I do it:

我是这样做的:

describe("myDirective", function() {
        it("should throw an error", inject(function ($compile, $rootScope) {
                function errorFunctionWrapper()
                {
                    $compile(angular.element("<div my-directive></div>"))($rootScope);
                }
                expect(errorFunctionWrapper).toThrow();
        }));
});

#2


7  

    it("should throw an error", inject(function ($compile, $rootScope) {
        expect(function () {
            $compile(angular.element("<directive-name></directive-name>"))($rootScope.$new());
        }).toThrow();
    }));

#3


2  

EDIT: this seems to be fixed now. Tested with Angular 1.6.4.

编辑:这似乎现在修复。用Angular 1.6.4测试。


In Angular 1.6, I'm been unable to catch errors thrown during the $compile phase. While not as elegant, I can still check against the $exceptionHandler.errors array (source):

在Angular 1.6中,我无法捕获$ compile阶段抛出的错误。虽然不那么优雅,但我仍然可以检查$ exceptionHandler.errors数组(源代码):

it('throws an error', function() {
  $compile(angular.element('<directive-name></directive-name>'))($rootScope.$new());
  expect($exceptionHandler.errors.length).toBeGreaterThan(0);
});

Better yet, provide it with the exact error message(s):

更好的是,为它提供确切的错误消息:

expect($exceptionHandler.errors).toEqual(['first error', 'second error'])

#4


0  

exception handling in angular testing is better done with native angular service

角度测试中的异常处理最好使用原生角度服务

https://docs.angularjs.org/api/ng/service/$exceptionHandler

https://docs.angularjs.org/api/ng/service/$exceptionHandler

this gives better handle on the thrown exceptions and provide a better native angular way to handle exception globally. say if at some point in time you can change the exception handling strategy of you application in a single place.

这样可以更好地处理抛出的异常,并提供更好的原生角度方式来全局处理异常。如果在某个时间点,您可以在一个地方更改应用程序的异常处理策略。

in testing this when used along with $exceptionHandlerProvider

与$ exceptionHandlerProvider一起使用时测试它

https://docs.angularjs.org/api/ngMock/provider/$exceptionHandlerProvider

https://docs.angularjs.org/api/ngMock/provider/$exceptionHandlerProvider

gives you a better handle on the generated exception and write specific tests.

为您提供更好的处理生成的异常并编写特定的测试。

for unit tests it is not a standard way in angular to validate an exception using .toThrow(); method of jasmine.

对于单元测试,使用.toThrow()验证异常不是角度的标准方法;茉莉花的方法。

#1


20  

This is how I do it:

我是这样做的:

describe("myDirective", function() {
        it("should throw an error", inject(function ($compile, $rootScope) {
                function errorFunctionWrapper()
                {
                    $compile(angular.element("<div my-directive></div>"))($rootScope);
                }
                expect(errorFunctionWrapper).toThrow();
        }));
});

#2


7  

    it("should throw an error", inject(function ($compile, $rootScope) {
        expect(function () {
            $compile(angular.element("<directive-name></directive-name>"))($rootScope.$new());
        }).toThrow();
    }));

#3


2  

EDIT: this seems to be fixed now. Tested with Angular 1.6.4.

编辑:这似乎现在修复。用Angular 1.6.4测试。


In Angular 1.6, I'm been unable to catch errors thrown during the $compile phase. While not as elegant, I can still check against the $exceptionHandler.errors array (source):

在Angular 1.6中,我无法捕获$ compile阶段抛出的错误。虽然不那么优雅,但我仍然可以检查$ exceptionHandler.errors数组(源代码):

it('throws an error', function() {
  $compile(angular.element('<directive-name></directive-name>'))($rootScope.$new());
  expect($exceptionHandler.errors.length).toBeGreaterThan(0);
});

Better yet, provide it with the exact error message(s):

更好的是,为它提供确切的错误消息:

expect($exceptionHandler.errors).toEqual(['first error', 'second error'])

#4


0  

exception handling in angular testing is better done with native angular service

角度测试中的异常处理最好使用原生角度服务

https://docs.angularjs.org/api/ng/service/$exceptionHandler

https://docs.angularjs.org/api/ng/service/$exceptionHandler

this gives better handle on the thrown exceptions and provide a better native angular way to handle exception globally. say if at some point in time you can change the exception handling strategy of you application in a single place.

这样可以更好地处理抛出的异常,并提供更好的原生角度方式来全局处理异常。如果在某个时间点,您可以在一个地方更改应用程序的异常处理策略。

in testing this when used along with $exceptionHandlerProvider

与$ exceptionHandlerProvider一起使用时测试它

https://docs.angularjs.org/api/ngMock/provider/$exceptionHandlerProvider

https://docs.angularjs.org/api/ngMock/provider/$exceptionHandlerProvider

gives you a better handle on the generated exception and write specific tests.

为您提供更好的处理生成的异常并编写特定的测试。

for unit tests it is not a standard way in angular to validate an exception using .toThrow(); method of jasmine.

对于单元测试,使用.toThrow()验证异常不是角度的标准方法;茉莉花的方法。