我正在使用angular和jasmine進行單元測試。
Result Message: Error: [$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;
}
};
});
最後,我引用文件:
01運行測試時,我收到以下錯誤/// <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" />
當我在測試中的注入函數中設置一個斷點並運行調試模式時,它將進入它的內核。
您的單元測試文件頂部的引用列表中沒有對您的控制器文件的引用。訂單也很重要,確保它是最後引用的文件。 – Igor
對不起,它在那裏,但我試圖嘗試一些東西。我仍然得到相同的錯誤 – steveareeno