1.beforeEach(inject(){...})中的代码会在beforeEach所在的describe中的每个it(包括子describe中的it)前执行一次。
2.假如it不是写在describe中而是写在另外一个it中则it中的内容不会被执行,且beforeEach不会执行。同理inject不是写在beforeEach中也不会被执行(能够写在it中执行)。
可能这里是使用了队列,在describe中执行it()时往队列里添,而写在it中的it在被执行时队列已经不接受加入了。
3.inject能够写在it中,甚至能写成it('', inject(...))
4.每一个inject会从新运行一次run。也会从新生成一次$rootScope
5.关于karma的假装响应。
使用$httpBackend.when(...).respond(200, data)来返回data。
假如要将本地html等文件做为上述data返回呢?
1. 首先须要利用$templateCache服务,本服务容许$http服务缓存通过XHR的模板请求。当一个模板被取到了,它的内容就会存储在$templateCache中,用模板路径作参数获取。
获取方式:$templateCache.get('xx.html')
2. 问题是如何将本地的xx.html放到$templateCache里呢?
咱们写代码当然能够经过$templateCache.put('xx.html', ...)来往里面放内容,但本地的文件须要将模板转换成可在测试中使用的ng模块。
解决方案为使用karma-ng-html2js-preprocessor包来作转换工做。
[以上参考 《AngularJS权威教程》 19.14.7 p268]
首先须要安装包:
$ npm install --save-dev karma-ng-html2js-preprocessor
而后须要修改karma.conf.js:
files: [
'**/*.html'
],
preprocessors: {
'**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'templates' // 这个是模块名字,随意取,
cacheIdFromPath: function(filepath){
return filepath // 这里获取到的filepath,是在上面files里定义的路径。然而这个路径与【被测试脚本】里所用的路径,可能由于根目录的不一样而有差别。因此能够在这里作处理转成与测试脚本同样的路径,这样在【测试代码】里就能用这个路径获取到html了,即$templateCache('xxx'),xxx与被测试脚本的路径一致。
},
stripPrefix: ''
}
而后在test.js里先写:
beforeEach(module('templates'))
beforeEach(module('mainApp'))
【注意,必定要先加载templates模块,再加载你的模块。若是反过来,而你的模块中run中动态编译指令,又使用了$digest要当即渲染,这时会直接须要获取模板文件,但因为templates模块未加载没法获取文件,会报错:Error: Unexpected request: GET test/myDirective.html】
【也许是因为模板直接写到$templateCache里,因此不须要经过设置$httpBackend.when来假装响应,更不须要$httpBackend.flush()】
而后这时假装响应就能在收到响应时返回文件
$httpBackend.when('GET', 'test/myDirective.html').respond(200, $templateCache.get('test/myDirective.html'))
【注意$httpBackend.when必须在被测试文件发起请求前设置】
在it中冲刷请求:
it('发送请求', function(){
$httpBackend.flush()
})
6. $httpBackend.flush()貌似只能使用一次。
it('1', function(){
$httpBackend.when(...).respond(...);$httpBackend.flush();
})
it('2', function(){
$httpBackend.when(...).respond(...);$httpBackend.flush();
})
这样会致使2里的when没法返回。改为如下则能够:
it...$http...respond(...)
it...$http...respond(...)
it...$httpBackend.flush()
7.$http的使用方法对$httpBackend.when有影响
$httpBackend.when以下:
$httpBackend.when('GET','/js/apps/main/tpl/questionEntry.html',function(){
console.log('a')
return true
})
.respond(200,$templateCache.get('/js/apps/main/tpl/questionEntry.html'))
而使用$http({method: 'GET', url:...})时,能按理console;使用$http.get('...')时,则不能console
8.假如请求的url后面有时间戳,怎么才能在$httpBackend里获取到data呢?这里彷佛从某个环节开始,都不会将时间戳做为data处理,因此致使when的第三个参数没法被调用。
解决方案以下:
$httpBackend.when('GET', {
test: function(url){
var str = '/js/apps/main/tpl/questionEntry.html'
return url.indexOf(str) != -1
}
})
.respond(200,$templateCache.get('/js/apps/main/tpl/questionEntry.html'))
请求使用directive、$http均可以发送。
9.在作inject注入时,若是屡次注入,要注意每次所获取到的服务都是全新的。因此当第二个inject里的服务与第一个inject里的服务混用时会产生问题。html