所以这是一个奇怪的,我已经设置了一个种子应用程序,我已经在控制器上创建了一个基本的测试脚本,它可以工作,但后来我移动到第二个,它给了我未定义的消息,我看到所有在互联网上,这里是两者的代码,因为我是业力茉莉的新手,有人可能会看到我在测试中缺少的东西
第一次测试,(这个有效)
//Controller
'use strict';
angular.module('myApp',[])// jshint ignore:line
.controller('HelloWorldController', ['$scope', function($scope) {
$scope.greeting = 'Hello World!';
}]);
//Test Script ***********************************************************
describe('Hello World example ', function() {
beforeEach(module('myApp'));
var HelloWorldController,
scope;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
HelloWorldController = $controller('HelloWorldController', {
$scope: scope
});
}));
it('says hello world!', function () {
expect(scope.greeting).toEqual("Hello World!");
});
});
然而,这个不起作用
// About Controller
'use strict';
angular.module('myApp.module.controller.about', ['ngRoute'])// jshint ignore:line
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/about', {
controller: 'aboutController',
templateUrl: 'com/modules/about/views/about.html',
hideMenus: true,
protectedArea: false,
title: 'About',
description: '',
keywords: ''
});
}])
.controller('aboutController', ['$scope', function($scope) {
$scope.greeting = 'This is the about message!';
}]);
//Test Script *****************************************
describe('AboutController Test', function() {
beforeEach(module('myApp.module.controller.about'));
var aboutController,
scope;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
aboutController = $controller('aboutController', {
$scope: scope
});
}));
it('about greeting says "This is the about message!"', function () {
expect(scope.greeting).toEqual("This is the about message!");
});
});
以下是我从Karma获得的内容
[12:58:47] Starting 'karma'...
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket I1XiLeRfz1SiV-wY2Y5x with id 89911408
PhantomJS 1.9.8 (Mac OS X 0.0.0) AboutController Test about greeting says "This is the about message!" FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=myApp.module.controller.about&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.3%2F%24injector%2Fmodulerr%3Fp0%3DngRoute%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.3%252F%2524injector%252Fnomod%253Fp0%253DngRoute%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A25%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A24)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A26%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A38%250A%2520%2520%2520%2520at%2520m%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A8)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A39)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A38%250A%2520%2520%2520%2520at%2520m%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A8)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A39)%250A%2520%2520%2520%2520at%2520eb%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A41)%250A%2520%2520%2520%2520at%2520workFn%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular-mocks.js%253F7822c97bd61398189431d2e3db4e01ed8f9d4f10%253A2391)%250A%2520%2520%2520%2520at%2520attemptSync%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1789)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1777%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1762%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A627%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A357%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A2360%250A%2520%2520%2520%2520at%2520attemptAsync%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1819)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1774%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1762%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A627%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A2354%250A%2520%2520%2520%2520at%2520attemptAsync%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1819)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1774%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1762%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A627%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A2215%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A678%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fkarma-jasmine%252Flib%252Fadapter.js%253F3030709c83121e1b2ca4d1e657306b834fc13350%253A318%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fkarma.js%253A182%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fcontext.html%253A75%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A39%0A%20%20%20%20at%20m%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A8)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A39)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A38%0A%20%20%20%20at%20m%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A8)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A39)%0A%20%20%20%20at%20eb%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A41)%0A%20%20%20%20at%20workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular-mocks.js%3F7822c97bd61398189431d2e3db4e01ed8f9d4f10%3A2391)%0A%20%20%20%20at%20attemptSync%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1789)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1777%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1762%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A627%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A357%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2360%0A%20%20%20%20at%20attemptAsync%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1819)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1774%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1762%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A627%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2354%0A%20%20%20%20at%20attemptAsync%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1819)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1774%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1762%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A627%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2215%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A678%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fadapter.js%3F3030709c83121e1b2ca4d1e657306b834fc13350%3A318%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fkarma.js%3A182%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fcontext.html%3A75
at /webdev/E21/src/lib/js/angular/angular.min.js:39
at m (/webdev/E21/src/lib/js/angular/angular.min.js:8)
at g (/webdev/E21/src/lib/js/angular/angular.min.js:39)
at eb (/webdev/E21/src/lib/js/angular/angular.min.js:41)
at workFn (/webdev/E21/src/lib/js/angular/angular-mocks.js:2391)
TypeError: 'undefined' is not an object (evaluating 'scope.greeting')
at /webdev/E21/src/com/modules/about/aboutController_spec.js:17
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 2 (1 FAILED) (0 secs / 0.007 secPhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 2 of 2 (1 FAILED) (0 secs / 0.01 secsPhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 2 of 2 (1 FAILED) (0.004 secs / 0.01 secs)
对此更熟悉的人可能会看到我缺少的东西,因为我知道第一次测试工作我知道我的设置是正确的所以问题必须在于关于控制器的实际测试规范,任何帮助将不胜感激。
答案 0 :(得分:0)
在玩完之后,我发现这有点具有欺骗性,问题根本不在规格中,规格有效,问题出在业力配置文件中,我已经包含了角度模拟但没有任何内容否则,一旦我将bower_components从仅仅模拟扩展到其他几个,我就能够完成所有测试并完成所有测试。
这是我为那些有类似问题的人所包含的内容(这是在karma.conf文件的文件区域下)
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'../src/com/**/*.js',
'../src/com/**/*_spec.js'