Running "karma:unit" (karma) task INFO [karma]: Karma v0.10.9 server started at http://localhost:9876/ INFO [launcher]: Starting browser PhantomJS INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket lJGhX9DakX2lChvLXtju WARN [web-server]: 404: /lang/en-US.json WARN [web-server]: 404: /api/v1/user/session WARN [web-server]: 404: /api/v1/user/auth
这就是我得到的……我的karma.conf.js有:
files: [ 'public/lib/angular/angular.js','public/lib/angular-mocks/angular-mocks.js','public/lib/angular-cookies/angular-cookies.js','public/lib/angular-resource/angular-resource.js','public/lib/angular-route/angular-route.js','public/lib/angular-strap/dist/angular-strap.min.js','public/lib/angular-animate/angular-animate.min.js','public/lib/angular-bootstrap/ui-bootstrap-tpls.js','public/lib/angular-bootstrap/ui-bootstrap.js','public/lib/angular-translate/angular-translate.min.js','public/lib/angular-translate-loader-static-files/angular-translate-loader-static-files.js','public/lib/angular-translate-storage-cookie/angular-translate-storage-cookie.js','public/js/**/*.js','test/karma/unit/**/*.js',{pattern: 'public/lang/*.*',included: false,served: true} ],
所以这应该包含lang文件,但它不包含.关于api调用,实际上并没有提供这些调用,但是我在我的app init中检查了一下,看看用户是否经过身份验证.在我的测试文件中,我正在做:
$httpBackend.when('GET','/api/v1/user/session').respond({status: 'ok'});
在我的beforeEach中,但这不是修复它.想法?
解决方法
如果您想在单元测试中跳过应用程序的配置部分,则需要将应用程序分成多个模块.这样,您可以在不运行其他模块的配置部分的情况下测试单个模块.
例如,如果您想在不运行应用程序配置部分的情况下对指令进行单元测试,则可以执行类似的操作.
var myAppModule = angular.module('myApp',['myAppDirectives']); myAppModule.config() { //This code won't be run in your unit tests. ... } var myAppDirectivesModule = angular.module('myAppDirectives',[]); myAppDirectivesModule.directive(/*Define your directives on this module*/);
然后在您导入模块的单元测试中,您只能导入指令模块:
beforeEach(module('myAppDirectives'));