Abr*_*m P 6 dependency-injection jasmine angularjs angularjs-scope angular-template
我一直在为一些Angular组件编写测试,使用我刚才在google上找到的语法:
describe('Directive: myDir', function () {
beforeEach(module('myApp'));
beforeEach(module('app/views/my_template.html'));
beforeEach(inject(function ($rootScope, _$compile_, $templateCache) {
$templateCache.put('views/my_template.html', $templateCache.get('app/views/my_template.html'));
var scope, $compile;
scope = $rootScope;
$compile = _$compile_;
element = angular.element("<div my-dir class='my-dir'></div>");
}));
it('does things', function () {
$compile(element)(scope);
scope.$digest();
});
});
Run Code Online (Sandbox Code Playgroud)
我的问题是关于注射的_$compile_.它与众不同之处$compile.我为什么要这样做呢?为什么$ compile被重新定义,为什么我不能简单地编译我注入的$ compile?
Mic*_*ord 12
从Angular官方教程(测试部分):
注入器在这里忽略前导和尾随下划线(即$ httpBackend).这允许我们注入服务,但然后将其附加到与服务同名的变量.
在您的示例中,您可以将变量重命名$compile为,compile然后从参数名称中删除下划线.事实上,你这样做是为了scope使$rootScope保持无下划线- .
我个人喜欢在测试中保留Angular内置服务的名称,以便在浏览代码时轻松发现它们.
| 归档时间: |
|
| 查看次数: |
2232 次 |
| 最近记录: |