j_b*_*ley 5 unit-testing jasmine angularjs
我正在使用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;
});
//...
}]);
Run Code Online (Sandbox Code Playgroud)
控制器..
myApp.controller('myController', function($scope, users) {
//...
});
Run Code Online (Sandbox Code Playgroud)
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);
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是整个错误消息......
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
小智 10
我不确定这对你有帮助,但是你可以试一试,我遇到了这个问题.我对AngularJS不是很好,所以如果这不起作用,我不知道该告诉你什么.在你的angular-mocks.js中找到函数isSpecRunning并将其更改为:
function isSpecRunning() {
//return currentSpec && (window.mocha || currentSpec.queue.running);
return !!currentSpec;
}
Run Code Online (Sandbox Code Playgroud)
我读了一些关于Jasmine 2.0的东西(不确定你是不是就是这样),除非你有这条线,否则不会表现出来.
他们在更新版本的angular-mocks.js(v 1.3.15)中使用上述逻辑解决了这个问题.