Jasmine Testing angularjs控制器

时间:2016-03-15 13:56:07

标签: angularjs unit-testing jasmine karma-jasmine

我正在尝试测试我的控制器但是在运行karma start时我得到了未定义

这是我的控制器代码:

(function() {
    'use strict';

    angular
        .module('tariffOptionsExtras')
        .controller('tariffOptionsExtrasBookCtrl', tariffOptionsExtrasBookCtrl);

    tariffOptionsExtrasBookCtrl.$inject = ['tariffOptionsExtrasSrv', '$location', 'cache', 'authSrv', '$stateParams',
        'tariffOptionsExtrasBookSrv', 'minimumDuration', '$rootScope', 'userTypes', 'messageCtrl', '$state'];

    function tariffOptionsExtrasBookCtrl(tariffOptionsExtrasSrv, $location, cache, authSrv, stateParams, tariffOptionsExtrasBookSrv, minimumDuration, $rootScope, userTypes, messageCtrl, $state) {
        var vm = this;
        var cacheName = 'tariffOptionsExtras';

        var cacheCollection = cache.getCollection(cacheName);
        vm.date = new Date();
        vm.minimumDuration = minimumDuration;
        //if statement commented out. prepaid users should be able to enter the flow.
Date(vm.bookOption.startDate)
        }
        if (!vm.bookOption) {
            $state.go(pagesConfig.tariffOptionsExtras.name);

        } else {
            vm.infoLink = vm.bookOption.infoUrl;
        }

        /**
         * Runs booking tarif extra post and in case of success the view is changed
         */
        vm.book = function() {
            //If bookoption not present, redirect to chooser
            tariffOptionsExtrasBookSrv.bookTariffExtra(vm.bookOption).then(function(response) {
                $rootScope.$broadcast('transactionFinished');
                var item = response['salesOrderVBO'][0]['orderItems']['orderItem'][0];
                if (item.product.action == 'BOOKED') {
                    vm.success = true;
                }
            }, function(reason) {
                vm.errorMessage = reason.data.faultMessage;
            });
        };
        vm.success = false;
        vm.subscription = authSrv.getUserContract();
        vm.msisdn = vm.subscription.subscription.msisdn;

    }
})();

使用茉莉花进行单元测试

describe('tariffOptionsExtras module', function() {


    describe('tariffOptionsExtrasBook Controller', function() {
        var tariffOptionsExtrasBookCtrl, tariffOptionsExtrasSrv, tariffOptionsExtrasBookSrv, authSrv;
        // Step 1: Import the module this controller belongs to, and its dependencies
        beforeEach(function() {
            module('app.common');
            module('tariffOptionsExtras');
        });

        // Step 2: Mock any service that initially used when the controller instantiate
        // beforeEach(module(function($provide) {

        //     $provide.factory('tariffOptionsExtrasBookSrv', function() {
        //         var getSync;
        //         getBookedExtras = function() {
        //             return {
        //                 then:function(){}
        //             };
        //         };
        //         getTariffBookableOptions = function() {
        //             return {
        //                 then:function(){}
        //             };
        //         };
        //         return {
        //             getBookedExtras: getBookedExtras,
        //             getTariffBookableOptions:getTariffBookableOptions
        //         };
        //     });
        // }));
        beforeEach(module(function($provide) {
            $provide.factory('authSrv', function() {
                getUserContract = function() {
                    return {
                        subscription:{
                            msisdn:'491741660390',
                            mboName:'27434975'
                        },
                        contract:{
                            mboName:'27434975',
                            ban:'106491816',
                        }
                    };
                };
                return {
                    getUserContract: getUserContract,
                };
            });

        }));


        // Step 3: Inject $controller with its dependencies
        beforeEach(function() {
            // 1. Import the module
            module('app');
            // 2. Inject $controller
            inject(function($controller, $rootScope, _authSrv_, _tariffOptionsExtrasSrv_, _tariffOptionsExtrasBookSrv_) {
                authSrv = _authSrv_;
                tariffOptionsExtrasSrv = _tariffOptionsExtrasSrv_;
                tariffOptionsExtrasBookSrv = _tariffOptionsExtrasBookSrv_;

                // 3. Use $controller to instantiate the controller
                tariffOptionsExtrasBookCtrl = $controller('tariffOptionsExtrasBookCtrl', {
                    'authSrv': authSrv,
                    'tariffOptionsExtrasSrv': tariffOptionsExtrasSrv,
                    'tariffOptionsExtrasBookSrv': tariffOptionsExtrasBookSrv
                });
            });
        });


        // Step 4: Test the controller
        it('Should return sum of 1+1', function() {
           expect(1+1).toBe(2);
        });

    });
});

当运行业力开始我得到这个屏幕: enter image description here

同样,当我评论这段代码时,它可以工作:

tariffOptionsExtrasBookCtrl = $controller('tariffOptionsExtrasBookCtrl', {
                'authSrv': authSrv,
                'tariffOptionsExtrasSrv': tariffOptionsExtrasSrv,
                'tariffOptionsExtrasBookSrv': tariffOptionsExtrasBookSrv
            });

1 个答案:

答案 0 :(得分:0)

首先你必须注入 $ controller param必须用下划线括起来 然后将它分配给$ controller变量,例如

beforeEach(inject(function ($rootScope, _$controller_) {
 scope = $rootScope.$new();
 $controller = _$controller_;
 $controller('nameController', {$scope: scope});
}));