我显示了这个错误,我在网上尝试了很多解决方案,就像http://www.benlesh.com/2013/06/angular-js-unit-testing-services.html中的这个示例一样,但似乎没有任何效果。
***** app.js
var app = angular.module('myApp', ['ui.router','ui.bootstrap','dialogs','ngResource']);
app.config(['$stateProvider', '$urlRouterProvider','$resourceProvider', function($stateProvider, $urlRouterProvider,$resourceProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url:'/',
views: {
......}})
}]);
***** test.js
describe('basicService tests', function (){
beforeEach(module('myApp'));
var basic;
// excuted before each "it" is run.
beforeEach(inject(function(basicService) {
basic = basicService;
}));
it('should make text exciting', function (){
//var result = basic.exciteText('bar');
expect(basic.exciteText('bar')).toBe('bar!');
});
});
***** service.js
angular.module('myApp').factory('basicService', function(){
return {
exciteText: function(msg) {
return msg + '!!!'
}
};
});
*****错误
minErr/<@C:/_Stage/myprojet/src/main/webapp/js/angular.js:63:12
loadModules/<@C:/_Stage/myprojet/src/main/webapp/js/angular.js:4138:15
forEach@C:/_Stage/myprojet/src/main/webapp/js/angular.js:323:11
loadModules@C:/_Stage/myprojet/src/main/webapp/js/angular.js:4099:5
createInjector@C:/_Stage/myprojet/src/main/webapp/js/angular.js:4025:11
workFn@C:/_Stage/myprojet/src/main/webapp/js/angular-mocks.js:2425:44
createStartFn/<@C:/_Stage/myprojet/src/main/webapp/js/adapter.js:317:5
TypeError: basic is undefined in C:/_Stage/myprojet/src/main/webapp/cportal/component/administration/caFingerPrint/caFingerprintService_test.js (line 20)
@C:/_Stage/myprojet/src/main/webapp/cportal/component/administration/caFingerPrint/caFingerprintService_test.js:20:9
createStartFn/<@C:/_Stage/myprojet/src/main/webapp/js/adapter.js:317:5
PS:在 karma.config.js 中,我添加了这些文件。 另外,我试过 var app = angular.module('myApp',[]); 因为我没有将它们包含在' karma.config.js '中像这样打电话给他们,
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
所以我知道它的错误,但没有。感谢任何帮助。
答案 0 :(得分:0)
似乎在karma.config.js中未正确设置所需的文件。使用以下karma.config.js时,我可以成功运行测试:
module.exports = function (config) {
var configuration = {
autoWatch: false,
frameworks: ['jasmine'],
files: [
'lib/angular.js',
'lib/angular-resource.js',
'lib/angular-ui-router.js',
'lib/angular-mocks.js',
'app.js',
'service.js',
'test/**/*js'
],
...
当然,您需要确保所有引用文件都在那里并且指定的路径(相对于karma.config)是正确的(在我的项目规范文件中是'test'文件夹,库 - 在'lib'中,应用程序文件与karma配置文件位于同一目录中)