使用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; }); //... }]); 控制器.. 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” 以下是我发现的错误的相关参考,表明它是Jasmine存在的问题.但是,在这种情况下,问题涉及Mocha,我没有使用. 解决方法
我不确定这对你有帮助,但是你可以试一试,我遇到了这个问题.我对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)中使用上述逻辑解决了这个问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |