AngularJs,Karma,Jasmine,为什么测试用例在这个场景下服务不可用?
AngularJs, Karma, Jasmine, Why are services not available under this scenario for test case?
我正在使用 grunt 和 运行 一个业力任务。我的单元测试用例配置为 karma/jasmine。似乎一切都应该到位,但我的一个服务不会加载。
下面的constanstsservice导致错误
(function() {
'use strict';
/**
* Run a suite of tests.
*/
describe('Registration Service API tests:', function() {
beforeEach(module('registration'));
var scope = null;
var constantService = null;
console.log('Setting up test : ');
/**
* Set up variables before each call.
* @returns
*/
beforeEach(inject(function($rootScope) {
scope = $rootScope.$new();
console.log('Before test[1]!! : ');
}));
beforeEach(inject(function($injector) {
var serv1 = $injector.get('ConstantsService');
console.log('Before test[2]!! : ');
}));
/**
* Tear down after each test.
* @returns
*/
afterEach(function() {
console.log('After test!! : ');
});
/**
* Run the behavior driven test.
*/
it('Test Constant API properties:', function() {
console.log('Running test!! : '+ angular.toJson(scope));
});
});
})();
错误:
[32mINFO [PhantomJS 1.9.8 (Windows 7)]: [39mConnected on socket M74ZRKv1ww3Od5Y0gbhR with id 86992083
[36mDEBUG [launcher]: [39mPhantomJS (id 86992083) captured in 2.23 secs
[36mDEBUG [web-server]: [39mserving: C:\someuser\projects\workspace\register\WebContent\node_modules\karma\static/context.html
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/jasmine.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/boot.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/adapter.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/jquery/dist/jquery.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-mocks/angular-mocks.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-ui-router/release/angular-ui-router.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-translate/angular-translate.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-animate/angular-animate.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/bootstrap/dist/js/bootstrap.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/moment/min/moment.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/js/base64.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/scripts/registration.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/scripts/services/applicationConstants.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/controllers/registrationCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/password/scripts/password.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/password/controllers/passwordCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/submodules/verify/scripts/controllers/verifyInformationCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/submodules/accountSetup/scripts/controllers/accountPasswordSetupCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/scripts/3c74702c.modules.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/scripts/progressbar.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/app.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/test/spec/registration/tests/applicationConstantTest.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/test/testHelper.js
PhantomJS 1.9.8 (Windows 7) LOG: [36m'Setting up test : '[39m
[1A[2KPhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2KLOG: [36m'Before test[1]!! : '[39m
PhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2KLOG: [36m'Running test!! : "$SCOPE"'[39m
PhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2KLOG: [36m'After test!! : '[39m
PhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2K[31mPhantomJS 1.9.8 (Windows 7) Registration Service API tests: Test Constant API properties: FAILED[39m
Error: [$injector:unpr] http://errors.angularjs.org/1.4.0/$injector/unpr?p0=ConstantsServiceProvider%20%3C-%20ConstantsService
at C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:40
at d (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:38)
at C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:40
at d (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:38)
at C:/someuser/projects/workspace/register/test/spec/registration/tests/applicationConstantTest.js?52375e25073be74d7060ae6c13510c057f544726:26
at e (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:38)
at workFn (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-mocks/angular-mocks.js:2420)
at C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/boot.js:117
at C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:10086/karma.js:189
at http://localhost:10086/context.html:81
undefined
PhantomJS 1.9.8 (Windows 7): Executed 1 of 1[31m (1 FAILED)[39m (0 secs / 0.014 secs)
[1A[2KPhantomJS 1.9.8 (Windows 7): Executed 1 of 1[31m (1 FAILED)[39m[31m ERROR[39m (0.002 secs / 0.014 secs)
[36mDEBUG [karma]: [39mRun complete, exitting.
[36mDEBUG [launcher]: [39mDisconnecting all browsers
[36mDEBUG [launcher]: [39mProcess PhantomJS exited with code 0
[36mDEBUG [temp-dir]: [39mCleaning temp dir C:\Users\User\AppData\Local\Temp\karma-86992083
[33mWarning: Task "karma:registration" failed. Use --force to continue.[39m
[31mAborted due to warnings.[39m
gruntfile.js相关:
karma: {
registration: {
configFile: 'karma.conf.js',
singleRun: true
}
},
业力配置文件:
module.exports = 函数(配置){
config.set({
// 启用/禁用监视文件并在任何文件更改时执行测试
autoWatch: false,
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/angular/angular.min.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-ui-router/release/angular-ui-router.min.js',
'app/bower_components/angular-translate/angular-translate.min.js',
'app/bower_components/angular-animate/angular-animate.min.js',
'app/bower_components/bootstrap/dist/js/bootstrap.js',
'app/bower_components/moment/min/moment.min.js',
'app/js/base64.js',
'app/modules/registration/scripts/registration.js',
'app/scripts/services/applicationConstants.js',
'app/modules/registration/controllers/registrationCtrl.js',
'app/modules/registration/submodules/verify/scripts/controllers/verifyInformationCtrl.js',
'app/modules/registration/submodules/accountSetup/scripts/controllers/accountPasswordSetupCtrl.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'app/modules/**/*.js',
'app/app.js',
'../test/**/*.js',
'app/scripts/services/applicationConstants.js',
],
这是服务,我要加载。我无法引用定义的服务。
(function() {
'use strict';
angular.module('registration').factory('ConstantsService', ['$q', function ($q) {
var appInformation = {
appname : 'app1'
};
/**
* Return encoded representation of application information.
*/
var encodeAppInformation = function() {
var dataResult = "000000";
var milliseconds = (new Date()).getTime();
if (!window.btoa) {
window.btoa = base64.encode(appInformation.appname);
}
dataResult = window.btoa(appInformation.appname+';'+milliseconds) ;
return dataResult;
};
return {
APPLICATION_INFORMATION: appInformation,
encodeAppInformation : encodeAppInformation
};
}]);
})();
不是在每个块之前注入 $injector
,而是注入服务本身,如下所示:
beforeEach(inject(function(ConstantsService) {
var serv1 = ConstantsService;
console.log('Before test[2]!! : ');
}));
问题也可能是您过多地包含了脚本并且您的模块定义被覆盖了,特别是在本节中
'app/scripts/*.js',
'app/scripts/**/*.js',
'app/modules/**/*.js',
'app/app.js',
'../test/**/*.js',
'app/scripts/services/applicationConstants.js',
如果您检查 angularjs 生成的 the error url,您可以看到以下模式可以消除注册:
angular.module('myModule', [])
.service('myCoolService', function () { /* ... */ });
angular.module('myModule', [])
// myModule has already been created! This is not what you want!
.directive('myDirective', ['myCoolService', function (myCoolService) {
// This directive definition throws unknown provider, because myCoolService
// has been destroyed.
}]);
这可能与您包含文件的方式有关
我正在使用 grunt 和 运行 一个业力任务。我的单元测试用例配置为 karma/jasmine。似乎一切都应该到位,但我的一个服务不会加载。
下面的constanstsservice导致错误
(function() {
'use strict';
/**
* Run a suite of tests.
*/
describe('Registration Service API tests:', function() {
beforeEach(module('registration'));
var scope = null;
var constantService = null;
console.log('Setting up test : ');
/**
* Set up variables before each call.
* @returns
*/
beforeEach(inject(function($rootScope) {
scope = $rootScope.$new();
console.log('Before test[1]!! : ');
}));
beforeEach(inject(function($injector) {
var serv1 = $injector.get('ConstantsService');
console.log('Before test[2]!! : ');
}));
/**
* Tear down after each test.
* @returns
*/
afterEach(function() {
console.log('After test!! : ');
});
/**
* Run the behavior driven test.
*/
it('Test Constant API properties:', function() {
console.log('Running test!! : '+ angular.toJson(scope));
});
});
})();
错误:
[32mINFO [PhantomJS 1.9.8 (Windows 7)]: [39mConnected on socket M74ZRKv1ww3Od5Y0gbhR with id 86992083
[36mDEBUG [launcher]: [39mPhantomJS (id 86992083) captured in 2.23 secs
[36mDEBUG [web-server]: [39mserving: C:\someuser\projects\workspace\register\WebContent\node_modules\karma\static/context.html
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/jasmine.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/boot.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/adapter.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/jquery/dist/jquery.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-mocks/angular-mocks.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-ui-router/release/angular-ui-router.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-translate/angular-translate.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-animate/angular-animate.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/bootstrap/dist/js/bootstrap.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/bower_components/moment/min/moment.min.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/js/base64.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/scripts/registration.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/scripts/services/applicationConstants.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/controllers/registrationCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/password/scripts/password.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/password/controllers/passwordCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/submodules/verify/scripts/controllers/verifyInformationCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/submodules/accountSetup/scripts/controllers/accountPasswordSetupCtrl.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/scripts/3c74702c.modules.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/modules/registration/scripts/progressbar.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/WebContent/app/app.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/test/spec/registration/tests/applicationConstantTest.js
[36mDEBUG [web-server]: [39mserving (cached): C:/someuser/projects/workspace/register/test/testHelper.js
PhantomJS 1.9.8 (Windows 7) LOG: [36m'Setting up test : '[39m
[1A[2KPhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2KLOG: [36m'Before test[1]!! : '[39m
PhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2KLOG: [36m'Running test!! : "$SCOPE"'[39m
PhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2KLOG: [36m'After test!! : '[39m
PhantomJS 1.9.8 (Windows 7): Executed 0 of 1[32m SUCCESS[39m (0 secs / 0 secs)
[1A[2K[31mPhantomJS 1.9.8 (Windows 7) Registration Service API tests: Test Constant API properties: FAILED[39m
Error: [$injector:unpr] http://errors.angularjs.org/1.4.0/$injector/unpr?p0=ConstantsServiceProvider%20%3C-%20ConstantsService
at C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:40
at d (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:38)
at C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:40
at d (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:38)
at C:/someuser/projects/workspace/register/test/spec/registration/tests/applicationConstantTest.js?52375e25073be74d7060ae6c13510c057f544726:26
at e (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular/angular.min.js:38)
at workFn (C:/someuser/projects/workspace/register/WebContent/app/bower_components/angular-mocks/angular-mocks.js:2420)
at C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/boot.js:117
at C:/someuser/projects/workspace/register/WebContent/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:10086/karma.js:189
at http://localhost:10086/context.html:81
undefined
PhantomJS 1.9.8 (Windows 7): Executed 1 of 1[31m (1 FAILED)[39m (0 secs / 0.014 secs)
[1A[2KPhantomJS 1.9.8 (Windows 7): Executed 1 of 1[31m (1 FAILED)[39m[31m ERROR[39m (0.002 secs / 0.014 secs)
[36mDEBUG [karma]: [39mRun complete, exitting.
[36mDEBUG [launcher]: [39mDisconnecting all browsers
[36mDEBUG [launcher]: [39mProcess PhantomJS exited with code 0
[36mDEBUG [temp-dir]: [39mCleaning temp dir C:\Users\User\AppData\Local\Temp\karma-86992083
[33mWarning: Task "karma:registration" failed. Use --force to continue.[39m
[31mAborted due to warnings.[39m
gruntfile.js相关:
karma: {
registration: {
configFile: 'karma.conf.js',
singleRun: true
}
},
业力配置文件:
module.exports = 函数(配置){ config.set({ // 启用/禁用监视文件并在任何文件更改时执行测试 autoWatch: false,
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/angular/angular.min.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-ui-router/release/angular-ui-router.min.js',
'app/bower_components/angular-translate/angular-translate.min.js',
'app/bower_components/angular-animate/angular-animate.min.js',
'app/bower_components/bootstrap/dist/js/bootstrap.js',
'app/bower_components/moment/min/moment.min.js',
'app/js/base64.js',
'app/modules/registration/scripts/registration.js',
'app/scripts/services/applicationConstants.js',
'app/modules/registration/controllers/registrationCtrl.js',
'app/modules/registration/submodules/verify/scripts/controllers/verifyInformationCtrl.js',
'app/modules/registration/submodules/accountSetup/scripts/controllers/accountPasswordSetupCtrl.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'app/modules/**/*.js',
'app/app.js',
'../test/**/*.js',
'app/scripts/services/applicationConstants.js',
],
这是服务,我要加载。我无法引用定义的服务。
(function() {
'use strict';
angular.module('registration').factory('ConstantsService', ['$q', function ($q) {
var appInformation = {
appname : 'app1'
};
/**
* Return encoded representation of application information.
*/
var encodeAppInformation = function() {
var dataResult = "000000";
var milliseconds = (new Date()).getTime();
if (!window.btoa) {
window.btoa = base64.encode(appInformation.appname);
}
dataResult = window.btoa(appInformation.appname+';'+milliseconds) ;
return dataResult;
};
return {
APPLICATION_INFORMATION: appInformation,
encodeAppInformation : encodeAppInformation
};
}]);
})();
不是在每个块之前注入 $injector
,而是注入服务本身,如下所示:
beforeEach(inject(function(ConstantsService) {
var serv1 = ConstantsService;
console.log('Before test[2]!! : ');
}));
问题也可能是您过多地包含了脚本并且您的模块定义被覆盖了,特别是在本节中
'app/scripts/*.js',
'app/scripts/**/*.js',
'app/modules/**/*.js',
'app/app.js',
'../test/**/*.js',
'app/scripts/services/applicationConstants.js',
如果您检查 angularjs 生成的 the error url,您可以看到以下模式可以消除注册:
angular.module('myModule', [])
.service('myCoolService', function () { /* ... */ });
angular.module('myModule', [])
// myModule has already been created! This is not what you want!
.directive('myDirective', ['myCoolService', function (myCoolService) {
// This directive definition throws unknown provider, because myCoolService
// has been destroyed.
}]);
这可能与您包含文件的方式有关