当我运行SpecRunner.html时,我收到以下错误
ReferenceError:未定义模块
我的控制器
angular.module('mymodule', [])
.controller('mycontroller', ['$scope',
function($scope) {
$scope.employees = [{
name: 'Dick',
address: 'Mumbai'
}, {
name: 'Tom',
address: 'US'
}];
$scope.addEmployee = function() {
$scope.employees.push({
name: $scope.name,
address: $scope.address
});
}
}
])

我的规格是
describe('Employee', function() {
var mycontroller, scope;
beforeEach(module('mymodule'));
beforeEach(inject(function($controller, $scope) {
scope = $rootScope;
mycontroller = $controller('mycontroller', {
$scope: scope
});
}));
it("when employee gets added", function() {
var employeecount = $scope.employees.count;
$scope.addEmployee('xxxx', 'yyy');
var employeecount1 = $scope.employees.count;
expect(employeecount + 1).toBe(employeecount1);
});
});

我的SpecRunner.html是
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.2.0/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">
<script src="lib/jasmine-2.2.0/jasmine.js"></script>
<script src="lib/jasmine-2.2.0/jasmine-html.js"></script>
<script src="lib/jasmine-2.2.0/boot.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="../Controller.js"></script>
<script src="spec/myspec.js"></script>
PS:这是我在茉莉花中的第一次单元测试。
答案 0 :(得分:4)
好的,所以我整天都在讨论这个问题,终于找到了为什么即使我在我的规范运行中有角度模拟脚本它也没有工作。这个命令很重要,这对我有用。
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.4.0/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.4.0/jasmine.css">
<script src="lib/jasmine-2.4.0/jasmine.js"></script>
<script src="lib/jasmine-2.4.0/jasmine-html.js"></script>
<script src="lib/jasmine-2.4.0/boot.js"></script>
<script src="../../lib/angular/angular.js"></script>
<script src="../../lib/angular/angular-animate.js"></script>
<script src="../../lib/angular/angular-route.js"></script>
<script src="../../lib/angular/angular-touch.js"></script>
<script src="../../lib/angular/angular-sanitize.js"></script>
<script src="../../lib/angular/angular-mocks.js"></script>
<!-- include source files here... -->
<script src="../student/data/classesData.js"></script>
<!-- include spec files here... -->
<script src="spec/LMSClassesSpec.js"></script>
我真的希望这可以帮助将来的某个人
答案 1 :(得分:3)
您正在引用测试代码中的window.module。您需要在spec-runner中加载角度模拟来执行此操作,请参阅https://docs.angularjs.org/api/ngMock/function/angular.mock.module