【问题标题】:AngularJS controller unit test with Jasmine使用 Jasmine 进行 AngularJS 控制器单元测试
【发布时间】:2014-08-07 20:29:52
【问题描述】:

我正在使用 Jasmine 对角度控制器进行单元测试,但我无法通过错误

“TypeError:无法读取未定义的属性'running'”。

完整的错误贴在底部。

这是应用定义...

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: Cannot read property 'running' of undefined at isSpecRunning (file:///C:/.../angular-mocks.js:1923:73) at window.inject.angular.mock.inject (file:///C:/.../angular-mocks.js:2087:20) 下一行指向注入函数 at Object.<anonymous> (file:///C:/.../mySpec.js:37:13) at attemptSync (file:///C:/.../jasmine.js:1510:12) at QueueRunner.run (file:///C:/.../jasmine.js:1498:9) at QueueRunner.execute (file:///C:/.../jasmine.js:1485:10) at Spec.Env.queueRunnerFactory (file:///C:/.../jasmine.js:518:35) at Spec.execute (file:///C:/.../jasmine.js:306:10) at Object.<anonymous> (file:///C:/.../jasmine.js:1708:37) at attemptAsync (file:///C:/.../jasmine.js:1520:12)

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

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:

    我不确定这是否会对您有所帮助,但您可以试一试,我遇到了这个问题。我对 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) 的较新版本中使用上述逻辑解决了此问题。

    【讨论】:

    • 谢谢,看起来可行。希望得到修复,我不喜欢更改任何角度文件
    • 这不可能是正确的解决方案...不是投反对票,但这似乎不对。
    • 我对 Jasmine 了解的有限知识是,他们似乎在那边有一个问题,除了解决方法之外还没有解决。我也不喜欢我自己更改文件的方法,但这正是我发现的解决方案。 github.com/pivotal/jasmine/issues/492 有一个很好的讨论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多