我目前正在研究将angular JS集成到Rails应用程序中的教程。
测试设置如下:
describe( 'Club functionality', function() {
// mock Application to allow us to inject our own dependencies
beforeEach(angular.mock.module('league'));
// create the custom mocks on the root scope
beforeEach(angular.mock.inject(function($rootScope, _$httpBackend_, $state){
//create an empty scope
scope = $rootScope.$new();
// we're just declaring the httpBackend here, we're not setting up expectations or when's - they change on each test
scope.httpBackend = _$httpBackend_;
scope.$state = $state;
}));
afterEach(function() {
scope.httpBackend.verifyNoOutstandingExpectation();
scope.httpBackend.verifyNoOutstandingRequest();
});
...
在完成了本节的内容并浏览了一些Angular文档之后,我仍然不清楚为什么在包含$ httpBackend依赖项时为什么要使用下划线。为什么这样嘲笑?scope.httpBackend = _$httpBackend_;
为了方便起见,我们希望像在应用程序中一样习惯在整个测试套件中引用我们的服务/范围。因此,我们需要将它们的引用保存在外部函数范围内。
首先,我们需要注入它们,因此我们尝试这样做时不带下划线:
var $httpBackend;
beforeEach(angular.mock.inject(function( $httpBackend ){
问题在于内部函数作用域变量$httpBackend
遮盖了外部函数作用域变量$httpBackend
,因此我们不能上层作用域链来将我们的引用设置在外部。
要对其进行修复,我们必须为内部和外部范围变量使用不同的名称。下划线只是$ injector的一点帮助,它可以轻松完成。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句