加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用Jasmine进行AngularJS控制器单元测试

发布时间:2020-12-17 06:50:43 所属栏目:安全 来源:网络整理
导读:我正在使用Jasmine对角度控制器进行单元测试,但我无法通过错误 “TypeError:无法读取未定义的’运行’属性”. 完整错误发布在底部. 这是应用程序定义…… var myApp= myApp|| angular.module('myApp',['ngRoute','ngSanitize','ui.bootstrap']);myApp.run([
我正在使用Jasmine对角度控制器进行单元测试,但我无法通过错误

“TypeError:无法读取未定义的’运行’属性”.

完整错误发布在底部.

这是应用程序定义……

var myApp= myApp|| angular.module('myApp',['ngRoute','ngSanitize','ui.bootstrap']);

myApp.run(['$http','$rootScope','properties',function($http,$rootScope,properties) {
    //...
    //Implementation of custom dependency
    properties.get().then(function(response) {
        $rootScope.propertiesLoaded = true;
        myApp.properties = response;
    });
   //...
}]);

控制器..

myApp.controller('myController',function($scope,users) {
    //...
});

test.js

describe("Test Controllers",function () {
    beforeEach(function () {
        angular.module("myApp");

        //Injection of mocked custom dependency into the myApp.run method
        myApp.run(function ($provide) {
            $provide.provider('properties',function () {
                this.$get = function () {
                    return "Mock return"
                };
            });
        });
    });

    describe("myController",function () {
        var scope,usrs,createMainController,mockDependency;

        beforeEach(function () {
            mockDependency = {
                current: {
                    get: function () {
                        return "Mock return";
                    }
                }
            };

            angular.module(function ($provide) {
                $provide.value('users',mockDependency);
            },[]);

            inject(function (_$injector_,_$controller_,_$rootScope_,users) {
                scope = _$rootScope_.$new();
                usrs = _$injector_.get("users");

                _$controller_("myController",{
                    $scope: scope,users: usrs
                });

                createMainController = function () {
                    return _$controller_("myController",{
                        $scope: scope,users: usrs
                    });
                };
            });

        });

        describe("This simple test",function () {
            it("should pass no matter what",function () {
                expect(true).toBe(true);
            });
        });
    });
});

这是整个错误消息……

TypeError:无法读取未定义的属性“running”
????at isSpecRunning(file:/// C:/…/angular-mocks.js:1923:73)
????在window.inject.angular.mock.inject(file:/// C:/…/angular-mocks.js:2087:20)
下一行指向注入功能
????at Object.< anonymous> (文件:/// C:/…/mySpec.js:37:13)
????at attemptSync(file:/// C:/…/jasmine.js:1510:12)
????在QueueRunner.run(file:/// C:/…/jasmine.js:1498:9)
????在QueueRunner.execute(file:/// C:/…/jasmine.js:1485:10)
????在Spec.Env.queueRunnerFactory(file:/// C:/…/jasmine.js:518:35)
????在Spec.execute(file:/// C:/…/jasmine.js:306:10)
????at Object.< anonymous> (文件:/// C:/…/jasmine.js:1708:37)
????at attemptAsync(file:/// C:/…/jasmine.js:1520:12)

以下是我发现的错误的相关参考,表明它是Jasmine存在的问题.但是,在这种情况下,问题涉及Mocha,我没有使用.
https://github.com/angular/angular.js/issues/1467

解决方法

我不确定这对你有帮助,但是你可以试一试,我遇到了这个问题.我对AngularJS不是很好,所以如果这不起作用,我不知道该告诉你什么.在你的angular-mocks.js中找到函数isSpecRunning并将其更改为:

function isSpecRunning() {
  //return currentSpec && (window.mocha || currentSpec.queue.running);
  return !!currentSpec;
}

我读了一些关于Jasmine 2.0的东西(不确定你是不是就是这样),除非你有这条线,否则不会表现出来.

他们在较新版本的angular-mocks.js(v 1.3.15)中使用上述逻辑解决了这个问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读