如何编写一个测试,希望在$ broadcast

时间:2017-01-11 09:31:06

标签: angularjs jasmine

我有一个带有switch语句的事件监听器,如果在switch语句中没有提供相关变量,则应该抛出错误。

我不确定如何在侦听器中抛出错误。

我的Jasmine代码

describe("event listner, 'somethingChanged'", function () {

            beforeEach(function () {

                $rootScope.$broadcast('somethingChanged', broadcastObject);

                broadcastObject= {
                    name: "Hello",
                    id: 123,
                    serviceTypeIdentifier: 0
                };
            });

            it("should throw an error if the broadcastObject's service type identifier has not been catered for", function () {

                expect(function () {

                    //...this is where I need to get a hold of the error being thrown somehow.

                }).toThrow();
            });
        });

事件监听器(角度代码)

$scope.$on("somethingChanged", function(event, args) {
                $scope.isBusy = true;
                broadcastObject = args;

                switch (broadcastObject.serviceTypeIdentifier) {
                    case 1:
                        //blah blah
                        break;
                    case 2:
                        //blah blah blah
                        break;
                    default:
                        $scope.isBusy = false;
                        throw "service type identifier of " + broadcastObject.serviceTypeIdentifier + " has not been catered for. Please verify the service ID of the object";
                }
        });

1 个答案:

答案 0 :(得分:2)

您必须将事件发送移至expect部分,例如

spec.js

describe('Controller: somethingChanged', function () {

    // load the controller's module
    beforeEach(module('something'));

    var $rootScope, scope;

    // Initialize the controller and a mock scope
    beforeEach(inject(function ($controller, _$rootScope_) {
        $rootScope = _$rootScope_;
        scope = $rootScope.$new();
        $controller('somethingChanged', {
            $scope: scope
        });
    }));

    it('throws error if service type identifier has not been provided', function () {
        expect(function() {
            $rootScope.$broadcast('somethingChanged', {});
        }).toThrow('service type identifier of undefined has not been catered for. Please verify the service ID of the object')
    });
});

如果控制器看起来如下,它将通过测试:

controller.js

angular.module('something', [])
    .controller('somethingChanged', function ($scope) {
        $scope.$on("somethingChanged", function (event, args) {
            $scope.isBusy = true;

            switch (args.serviceTypeIdentifier) {
                case 1:
                    //blah blah
                    break;
                case 2:
                    //blah blah blah
                    break;
                default:
                    $scope.isBusy = false;
                    throw "service type identifier of " + args.serviceTypeIdentifier + " has not been catered for. Please verify the service ID of the object";
            }
        });
    });

注意:当您学习示例时,请查看参数_$rootScope_如何分配给变量$rootScope。它是内部AngularJS机制的一部分,用于从变量名中去除所有_个符号。