“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
解决方法
function isSpecRunning() { //return currentSpec && (window.mocha || currentSpec.queue.running); return !!currentSpec; }
我读了一些关于Jasmine 2.0的东西(不确定你是不是就是这样),除非你有这条线,否则不会表现出来.
他们在较新版本的angular-mocks.js(v 1.3.15)中使用上述逻辑解决了这个问题.