0
我已經安裝並重新安裝了咕嚕聲,業力和茉莉花,並仍然存在以下問題。如果我在Jasmine獨立運行測試,測試工作正常。似乎不是路徑問題。如果我grunt karma
命令來運行它,我得到的錯誤如下:異常運行角茉莉測試與咕嚕業餘
這是我Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
configFile: 'c:/tools/karma.conf.js',
autoWatch: true
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
這是我karma.conf.js:
module.exports = function(config) {
config.set({
basePath: 'c:/tools/public/',
frameworks: ['jasmine'],
files: [
'libs/angular/angular.min.js',
'libs/angular/angular-mocks.js',
'test/cookbookspec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: true,
concurrency: Infinity
})
}
而且這是我得到的錯誤:
C:\tools\npm>grunt karma
Running "karma:unit" (karma) task
01 01 2017 13:17:52.333:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
01 01 2017 13:17:52.333:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
01 01 2017 13:17:52.364:INFO [launcher]: Starting browser PhantomJS
01 01 2017 13:17:53.883:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#9TpClXEcWPipDleTAAAA with id 35045843
PhantomJS 2.1.1 (Windows 8 0.0.0) cookbook level 1 cookbook level 2 should assign the correct rapper to scope FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=cookbook&p1=%5B%24injector%3Anomod%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.8%2F%24injector%2Fnomod%3Fp0%3Dcookbook%0Ahttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A24%3A198%0Ab%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A23%3A252%0Ahttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A23%3A707%0Ahttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A38%3A119%0An%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A7%3A337%0Ag%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A37%3A562%0Aeb%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A41%3A250%0AworkFn%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular-mocks.js%3F41fb2d656d9a871440ee139e6491d0e2b124ebc8%3A2428%3A60%0AattemptSync%40http%3A%2F%2Flocalhost%3A9876%2FC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1950%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1938%3A20%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A371%3A28%0Afn%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2579%3A44%0AattemptAsync%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1980%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1935%3A21%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Afn%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2564%3A31%0AattemptAsync%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1980%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1935%3A21%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Afn%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2564%3A31%0AattemptAsync%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1980%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1935%3A21%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2426%3A25%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A776%3A24%0Ahttp%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fadapter.js%3F7a813cc290d592e664331c573a1a796192cdd1ad%3A336%3A23%0Aloaded%40http%3A%2F%2Flocalhost%3A9876%2Fcontext.js%3A151%3A17%0Aglobal%20code%40http%3A%2F%2Flocalhost%3A9876%2Fcontext.html%3A44%3A28 in libs/angular/angular.min.js (line 38)
libs/angular/angular.min.js:38:429
[email protected]/angular/angular.min.js:7:337
[email protected]/angular/angular.min.js:37:562
[email protected]/angular/angular.min.js:41:250
[email protected]/angular/angular-mocks.js:2428:60
[email protected]://localhost:9876/context.js:151:17
TypeError: undefined is not a constructor (evaluating '$controller('MainCtrl', {
$scope: $scope
})') in test/cookbookspec.js (line 16)
test/cookbookspec.js:16:32
[email protected]://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.016 secs/0.009 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
UPDATE
安裝AngularJS的不精縮版後,我得到以下信息:
C:\tools\npm>grunt karma
Running "karma:unit" (karma) task
01 01 2017 13:39:53.734:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
01 01 2017 13:39:53.734:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
01 01 2017 13:39:53.734:INFO [launcher]: Starting browser PhantomJS
01 01 2017 13:39:55.283:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#wB8sLyHoRK3D3WWhAAAA with id 75708888
PhantomJS 2.1.1 (Windows 8 0.0.0) cookbook level 1 cookbook level 2 should assign the correct rapper to scope FAILED
[email protected]/angular.js:340:24
[email protected]/angular.js:4419:12
[email protected]/angular.js:4344:22
[email protected]/angular/angular-mocks.js:2428:60
[email protected]://localhost:9876/context.js:151:17
test/angular.js:4459:53
TypeError: undefined is not a constructor (evaluating '$controller('MainCtrl', {
$scope: $scope
})') in test/cookbookspec.js (line 16)
test/cookbookspec.js:16:32
[email protected]://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0 secs/0.009 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
控制器我測試:
angular.module('cookbook', [])
.controller('MainCtrl', function($scope) {
$scope.sum = function() {
$scope.result = $scope.x + $scope.y;
}
});
而且茉莉花測試:
describe('cookbook level 1', function() {
beforeEach(angular.mock.module('cookbook'));
var $controller;
beforeEach(angular.mock.inject(function(_$controller_){
$controller = _$controller_;
}));
describe('cookbook level 2', function() {
it('should assign the correct rapper to scope', function() {
var $scope = {};
var controller = $controller('MainCtrl', {
$scope: $scope
});
$scope.x = 1;
$scope.y = 2;
$scope.sum();
expect($scope.result).toBe(1+2);
});
});
不要在開發過程中使用縮小版本的角度,以獲得可讀的錯誤消息。併發布相關代碼。我發現可疑的是你要加載的唯一文件是一個spec文件。那麼...你正在測試的代碼在哪裏(即MainCtrl)? –
謝謝,請參閱更新 – ps0604
再次說明:您沒有在您的karma配置中加載JS文件。 –