简体   繁体   English

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

[英]How to mock objects inside $scope or $window in AngularJs tests using Karma and Jasmine?

I am new to testing AngularJs controllers with Karma and Jasmine. 我刚开始使用Karma和Jasmine测试AngularJs控制器。

I'm trying to test this controller: 我正在尝试测试此控制器:

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;
        }

Here's my test code: 这是我的测试代码:

'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();
        });
    });
});

However, when I try to execute this test code I get this error: 但是,当我尝试执行此测试代码时,出现以下错误:

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

I have tried to mock the $window object and overriding angular's $window object, but I wasn't successful. 我试图模拟$window对象并覆盖angular的$ window对象,但是我没有成功。

I have checked the dependencies in my karma.conf.js file and they're all there. 我已经检查了我的karma.conf.js文件中的依赖项,它们都在那里。

I have also checked these questions: 我还检查了以下问题:

Karma-Jasmine: How to test $translate.use? 业力茉莉花:如何测试$ translate.use?

jasmine mock window object 茉莉花模拟窗口对象

but the proposed solutions didn't really help. 但是建议的解决方案并没有真正帮助。

Thus, I'm trying to find a way to mock the $scope.translation['admin_platform_init'] object in order to be able to execute my tests. 因此,我试图找到一种方法来模拟$scope.translation['admin_platform_init']对象,以便能够执行我的测试。

Can someone please point me in the right direction? 有人可以指出正确的方向吗?

Thank you. 谢谢。

try this instead of $window.translation 试试这个而不是$ window.translation

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

I managed to solve my problem by importing the 'app' module. 我设法通过导入“ app”模块解决了我的问题。

The test code after the fix looks like this: 修复后的测试代码如下:

'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();
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM