【发布时间】:2016-02-08 17:46:21
【问题描述】:
我正在使用 Angular 和 jasmine 进行单元测试。运行测试时出现以下错误:
结果消息:错误:[$injector:modulerr] http://errors.angularjs.org/1.4.9/$injector/modulerr...
单元测试
/// <reference path="../Scripts/_references.js" />
/// <reference path="jasmine.js" />
/// <reference path="SampleController.js" />
describe('Controller Unit Test: ', function () {
var scope, ctrl, $timeout;
var someServiceMock;
beforeEach(function () {
module('app');
});
beforeEach(function () {
someServiceMock = jasmine.createSpyObj('someService', ['someAsyncCall']);
inject(function ($rootScope, $controller, $q, _$timeout_) {
scope = $rootScope.$new();
someServiceMock.someAsyncCall.andReturn($q.when('weee'));
$timeout = _$timeout_;
ctrl = $controller('SampleController', {
$scope: scope,
someService: someServiceMock
});
});
});
it('Controller exists', function () {
expect(ctrl).toBeDefined();
});
});
控制器
var app = angular.module('myApp', []);
app.controller('SampleController', function ($scope, someService) {
//set some properties
$scope.foo = 'foo';
$scope.bar = 'bar';
//add a simple function.
$scope.test1 = function () {
$scope.foo = $scope.foo + '!!!';
};
//set up a $watch.
$scope.$watch('bar', function (v) {
$scope.baz = v + 'baz';
});
//make a call to an injected service.
$scope.test2 = function () {
someService.someAsyncCall($scope.foo)
.then(function (data) {
$scope.fizz = data;
});
};
});
app.factory('someService', function ($timeout, $q) {
return {
// simple method to do something asynchronously.
someAsyncCall: function (x) {
var deferred = $q.defer();
$timeout(function () {
deferred.resolve(x + '_async');
}, 100);
return deferred.promise;
}
};
});
最后,我的参考文件:
/// <reference path="angular.min.js" />
/// <reference path="angular-mocks.js" />
/// <reference path="angular-animate.min.js" />
/// <reference path="angular-sanitize.min.js" />
/// <reference path="angular-route.min.js" />
/// <reference path="jquery-2.1.1.min.js" />
/// <reference path="jquery.unobtrusive-ajax.js" />
/// <reference path="jquery.unobtrusive-ajax.min.js" />
/// <reference path="jquery.validate.min.js" />
/// <reference path="ui-bootstrap-tpls-0.14.3.min.js" />
/// <reference path="modernizr-2.8.3.js" />
当我在测试中的注入函数中设置断点并运行调试模式时,它永远不会进入它。
【问题讨论】:
-
您的单元测试文件顶部的引用列表中没有对控制器文件的引用。顺序也很重要,请确保它是最后引用的文件。
-
对不起,它在里面,但我把它拿出来尝试一下。我仍然得到同样的错误
标签: angularjs unit-testing jasmine