我正在尝试学习如何进行单元测试,我有一个简单的测试场景,有两种情况,我会立即向您展示代码。 第一个测试运行正常,但第二个测试给出了这个错误:
Error: [ng:areq] http://errors.angularjs.org/1.5.0-beta.2/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20stringI/<@http://localhost:63342/happiness_module/node_modules/angular/angular.min.js:6:423
rb@http://localhost:63342/happiness_module/node_modules/angular/angular.min.js:22:108
Ra@http://localhost:63342/happiness_module/node_modules/angular/angular.min.js:22:195
eb.$$annotate@http://localhost:63342/happiness_module/node_modules/angular/angular.min.js:189:216
angular.injector.$$annotate@http://localhost:63342/happiness_module/node_modules/angular-mocks/angular-mocks.js:2461:12
e@http://localhost:63342/happiness_module/node_modules/angular/angular.min.js:40:306
workFn/<@http://localhost:63342/happiness_module/node_modules/angular-mocks/angular-mocks.js:2544:13
n@http://localhost:63342/happiness_module/node_modules/angular/angular.min.js:7:342
workFn@http://localhost:63342/happiness_module/node_modules/angular-mocks/angular-mocks.js:2533:9
以下是test.html文件:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<!-- library scripts -->
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/angular/angular.min.js"></script>
<script>
mocha.setup({
"ui": "bdd",
"reporter": "html"
});
</script>
<script src="../node_modules/angular-mocks/angular-mocks.js"></script>
<!-- project scripts -->
<script src="../js/app.js"></script>
<script src="../js/services/SimpleService.js"></script>
<script src="../js/controllers/MyController.js"></script>
<script src="test.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
mocha.run();
</script>
</body>
</html>
app.js:
angular
.module('app', []);
myController的:
angular.module('app').controller('MyController', [ '$scope', function ($scope) {
$scope.doSomething = function() {
return 10;
}
$scope.doOtherThing = function() {
return 11;
}
}]);
test.js:
window.assert = chai.assert;
var expect = chai.expect;
beforeEach(module('app'));
describe('MyController', function () {
var ctrl,
scope;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('MyController', {$scope: scope});
}));
describe('First Test', function() {
it('Do something should be 10', function () {
var result = scope.doSomething();
expect(result).to.equal(10);
});
});
describe('Second Test', function() {
it('Do another thing should be 11', function () {
var result = scope.doOtherThing();
expect(result).to.equal(11);
});
});
});
有谁可以告诉我,我做错了什么?