我目前正在研究一个将角度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,所以我们不能上升范围链来设置我们的引用外部.
要修复它,我们必须为内部和外部范围变量使用不同的名称.这些下划线只是从$注射器的一点帮助,没有痛苦.