如何使用Karma和Jasmine在AngularJs测试中模拟$ scope或$ window中的对象?

时间:2017-02-02 19:09:52

标签: javascript angularjs unit-testing jasmine karma-jasmine

我是使用Karma和Jasmine测试AngularJs控制器的新手。

我正在尝试测试这个控制器:

angular.module('app.dashboard.admin', [])
    .controller('AdminCtrl', function (locale, $log, $scope, $window, $state) {

        $scope.translation = $window.translation()[locale];
        $scope.showAdminBoard = false;
        $scope.initModel = {
            disableProgress: false,
            message: $scope.translation['admin_platform_init'],
            error: ''
        };

        $scope.adminPrivileges = {};

        $scope.onGetAdminPrivileges = function () {
            return $scope.adinPrivileges;
        }

这是我的测试代码:

'use strict';

describe('dashboard.admin module', function () {
    beforeEach(function(){
        module('app.dashboard.admin');
    });

    var auth, scope, ctrl, window;

    beforeEach(inject(function ($controller, $rootScope, $window) {
        auth = Auth;
        scope = $rootScope.$new(); //get a childscope
        window = {
            translation: $window.translation
        };

        ctrl = $controller("AdminCtrl", {$scope: scope, $window: window});
    }));

    describe('Admin Controller', function () {
        it('should inject controller', function () {
            expect(ctrl).toBeDefined();
        });
    });
});

但是,当我尝试执行此测试代码时,我收到此错误:

TypeError: undefined is not an object (evaluating '$scope.translation['admin_platform_init']') (line 11)
        views/dashboard.admin/admin.js:11:40
        [native code]
        instantiate@bower_components/angular/angular.js:4786:61
        $controller@bower_components/angular/angular.js:10607:39
        bower_components/angular-mocks/angular-mocks.js:2249:23
        views/dashboard.admin/admin.spec.js:113:27
        invoke@bower_components/angular/angular.js:4771:24
        WorkFn@bower_components/angular-mocks/angular-mocks.js:3130:26
        loaded@http://localhost:9876/context.js:151:17
        inject@bower_components/angular-mocks/angular-mocks.js:3097:28
        views/dashboard.admin/admin.spec.js:106:22
        global code@views/dashboard.admin/admin.spec.js:3:9
        Expected undefined to be defined.
        views/dashboard.admin/admin.spec.js:118:37
        loaded@http://localhost:9876/context.js:151:17

我试图模仿$window对象并覆盖angular的$ window对象,但我没有成功。

我已经检查了karma.conf.js文件中的依赖项,它们都在那里。

我也检查了这些问题:

Karma-Jasmine: How to test $translate.use?

jasmine mock window object

但建议的解决方案并没有真正帮助。

因此,我试图找到一种模拟$scope.translation['admin_platform_init']对象的方法,以便能够执行我的测试。

有人可以指出我正确的方向吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个而不是$ window.translation

window = {
    translation: function () {
        return {
            "admin_platform_init": "This is test message"
        };
    }
};

答案 1 :(得分:0)

我设法通过导入' app'来解决我的问题。模块。

修复后的测试代码如下所示:

'use strict';

describe('dashboard.admin module', function () {
    beforeEach(function(){
        module('app');
        module('app.dashboard.admin');
    });

    var auth, scope, ctrl, window;

    beforeEach(inject(function ($controller, $rootScope, $window) {
        auth = Auth;
        scope = $rootScope.$new(); //get a childscope
        window = {
            translation: $window.translation
        };

        ctrl = $controller("AdminCtrl", {$scope: scope, $window: window});
    }));

    describe('Admin Controller', function () {
        it('should inject controller', function () {
            expect(ctrl).toBeDefined();
        });
    });
});