我正在开发一个相当复杂的angularjs应用程序。我有一项服务,我想通过业力+ jasmin测试;但如果它适用于测试,它会崩溃app和visceversa(如果应用程序有效,测试失败)。
唯一更改的行是服务声明中的这一行:
var app = angular.module('inspinia', []); //test works - app crashes
var app = angular.module('inspinia'); //app works, test crashes
测试失败时出错:
$ karma start
03 02 2016 22:56:35.038:DEBUG [plugin]: Loading karma-* from /usr/local/lib/node_modules
03 02 2016 22:56:35.041:DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-jasmine.
03 02 2016 22:56:35.042:DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-phantomjs-launcher.
03 02 2016 22:56:35.144:DEBUG [karma]: List of files has changed, trying to execute
03 02 2016 22:56:35.144:WARN [karma]: No captured browser, open http://localhost:9876/
03 02 2016 22:56:35.149:DEBUG [watcher]: Watching "/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js"
03 02 2016 22:56:35.150:DEBUG [watcher]: Watching "/home/eduardo/ventamia/vm2/clientside/www-app/node_modules/angular-mocks/angular-mocks.js"
03 02 2016 22:56:35.150:DEBUG [watcher]: Watching "/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/ngStorage.min.js"
03 02 2016 22:56:35.150:DEBUG [watcher]: Watching "/home/eduardo/ventamia/vm2/clientside/www-app/js/app.js"
03 02 2016 22:56:35.150:DEBUG [watcher]: Watching "/home/eduardo/ventamia/vm2/clientside/www-app/js/app-services"
03 02 2016 22:56:35.150:DEBUG [watcher]: Watching "/home/eduardo/ventamia/vm2/clientside/www-app/js/app-services-tests"
03 02 2016 22:56:35.153:INFO [karma]: Karma v0.13.19 server started at http://localhost:9876/
03 02 2016 22:56:35.158:INFO [launcher]: Starting browser PhantomJS
03 02 2016 22:56:35.158:DEBUG [temp-dir]: Creating temp dir at /tmp/karma-22744193
03 02 2016 22:56:35.161:DEBUG [launcher]: /usr/local/lib/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs /tmp/karma-22744193/capture.js
03 02 2016 22:56:35.269:DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/client.html
03 02 2016 22:56:35.279:DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/karma.js
03 02 2016 22:56:35.315:DEBUG [karma]: A browser has connected on socket /#p4GAtSgYr6JygWKCAAAA
03 02 2016 22:56:35.329:DEBUG [web-server]: upgrade /socket.io/?EIO=3&transport=websocket&sid=p4GAtSgYr6JygWKCAAAA
03 02 2016 22:56:35.401:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#p4GAtSgYr6JygWKCAAAA with id 22744193
03 02 2016 22:56:35.401:DEBUG [launcher]: PhantomJS (id 22744193) captured in 0.248 secs
03 02 2016 22:56:35.401:DEBUG [karma]: All browsers are ready, executing
03 02 2016 22:56:35.408:DEBUG [phantomjs.launcher]:
03 02 2016 22:56:35.411:DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/context.html
03 02 2016 22:56:35.415:DEBUG [middleware:source-files]: Requesting /absolute/usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?391e45351df9ee35392d2e5cb623221a969fc009 /
03 02 2016 22:56:35.415:DEBUG [middleware:source-files]: Fetching /usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
03 02 2016 22:56:35.415:DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
03 02 2016 22:56:35.416:DEBUG [middleware:source-files]: Requesting /base/js/app.js?eb7b94b12612a8224fa3e53352074dd486839a7a /
03 02 2016 22:56:35.416:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app.js
03 02 2016 22:56:35.417:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app.js
03 02 2016 22:56:35.417:DEBUG [middleware:source-files]: Requesting /absolute/usr/local/lib/node_modules/karma-jasmine/lib/boot.js?4a7da64f416169520c9d5c43b5a7feac6bde9104 /
03 02 2016 22:56:35.417:DEBUG [middleware:source-files]: Fetching /usr/local/lib/node_modules/karma-jasmine/lib/boot.js
03 02 2016 22:56:35.417:DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/boot.js
03 02 2016 22:56:35.418:DEBUG [middleware:source-files]: Requesting /base/js/app-services/authentication.service.js?e29ac74d743ad6ed152315f8e02120a82d82e77b /
03 02 2016 22:56:35.418:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/authentication.service.js
03 02 2016 22:56:35.419:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/authentication.service.js
03 02 2016 22:56:35.419:DEBUG [middleware:source-files]: Requesting /base/js/app-services/flash.service.js?2f379ae1f884b24f952a237a8f08da435496e318 /
03 02 2016 22:56:35.419:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/flash.service.js
03 02 2016 22:56:35.420:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/flash.service.js
03 02 2016 22:56:35.420:DEBUG [middleware:source-files]: Requesting /absolute/usr/local/lib/node_modules/karma-jasmine/lib/adapter.js?3030709c83121e1b2ca4d1e657306b834fc13350 /
03 02 2016 22:56:35.420:DEBUG [middleware:source-files]: Fetching /usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
03 02 2016 22:56:35.421:DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
03 02 2016 22:56:35.421:DEBUG [middleware:source-files]: Requesting /base/js/angular/angular.js?fa0d4b85854663a64407ea33e2b58bf05097db6c /
03 02 2016 22:56:35.421:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js
03 02 2016 22:56:35.421:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js
03 02 2016 22:56:35.431:DEBUG [middleware:source-files]: Requesting /base/node_modules/angular-mocks/angular-mocks.js?e2829afc9f48dbc70d62e76c093673c90c0b575a /
03 02 2016 22:56:35.431:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/node_modules/angular-mocks/angular-mocks.js
03 02 2016 22:56:35.432:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/node_modules/angular-mocks/angular-mocks.js
03 02 2016 22:56:35.433:DEBUG [middleware:source-files]: Requesting /base/js/angular/ngStorage.min.js?a34e7d4a74c2178e0ad53f22b1a54c9990d4f754 /
03 02 2016 22:56:35.433:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/angular/ngStorage.min.js
03 02 2016 22:56:35.433:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/angular/ngStorage.min.js
03 02 2016 22:56:35.434:DEBUG [middleware:source-files]: Requesting /base/js/app-services/product.service.js?ca614815bdff50e4e2a9bda4589cd7821dee9a09 /
03 02 2016 22:56:35.435:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/product.service.js
03 02 2016 22:56:35.435:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/product.service.js
03 02 2016 22:56:35.435:DEBUG [middleware:source-files]: Requesting /base/js/app-services/user.service.js?b8609883742ce66f0f9b737cc39175e29dad275d /
03 02 2016 22:56:35.435:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/user.service.js
03 02 2016 22:56:35.436:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/user.service.js
03 02 2016 22:56:35.436:DEBUG [middleware:source-files]: Requesting /base/js/app-services/user.service.local-storage.js?4ec7890d764873e39fe32b714172879e88bf5945 /
03 02 2016 22:56:35.436:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/user.service.local-storage.js
03 02 2016 22:56:35.436:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services/user.service.local-storage.js
03 02 2016 22:56:35.437:DEBUG [middleware:source-files]: Requesting /base/js/app-services-tests/product.service.test.js?c6dfbaff24458a7be8479a1d495fdd769756f5f2 /
03 02 2016 22:56:35.437:DEBUG [middleware:source-files]: Fetching /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services-tests/product.service.test.js
03 02 2016 22:56:35.437:DEBUG [web-server]: serving (cached): /home/eduardo/ventamia/vm2/clientside/www-app/js/app-services-tests/product.service.test.js
PhantomJS 2.1.1 (Linux 0.0.0) ProductService can get an instance of my service FAILED
/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:4116:53
forEach@/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:323:24
loadModules@/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:4076:12
createInjector@/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:4002:22
workFn@/home/eduardo/ventamia/vm2/clientside/www-app/node_modules/angular-mocks/angular-mocks.js:2506:60
/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:4116:53
forEach@/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:323:24
loadModules@/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:4076:12
createInjector@/home/eduardo/ventamia/vm2/clientside/www-app/js/angular/angular.js:4002:22
workFn@/home/eduardo/ventamia/vm2/clientside/www-app/node_modules/angular-mocks/angular-mocks.js:2506:60
应用崩溃时出错:
Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined
http://errors.angularjs.org/1.3.7/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
at angular.js:63
at assertArg (angular.js:1575)
at assertArgFn (angular.js:1585)
at angular.js:8416
at angular.js:7590
at forEach (angular.js:331)
at nodeLinkFn (angular.js:7577)
at compositeLinkFn (angular.js:7073)
at compositeLinkFn (angular.js:7076)
at publicLinkFn (angular.js:6952)
这是我的服务的高度简化版本(但足以重现错误):
var app = angular.module('inspinia', []);
app.factory('ProductService', ['$rootScope', '$http', '$localStorage', function($rootScope, $http, $localStorage){
this.SayHi = function(){
return "Hi";
};
return this;
}]);
这是我的单元测试(也简化):
describe('ProductService', function () {
beforeEach(module('inspinia'));
beforeEach(module('ngStorage'));
beforeEach(inject(function($localStorage) {
$localStorage = $localStorage;
}));
it('can get an instance of my factory', inject(function(ProductService) {
expect(ProductService).toBeDefined();
}));
});
所以我有两个问题: 1.我的模块声明之间有什么区别 2.我可以做些什么让我的测试和应用程序正常工作?