加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular JS测试依赖注入中下划线的要点是什么

发布时间:2020-12-17 09:41:44 所属栏目:安全 来源:网络整理
导读:我目前正在研究一个将角度JS集成到Rails应用程序中的教程. 测试设置如下: describe( 'Club functionality',function() { // mock Application to allow us to inject our own dependencies beforeEach(angular.mock.module('league')); // create the custo
我目前正在研究一个将角度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,所以我们不能上升范围链来设置我们的引用外部.

要修复它,我们必须为内部和外部范围变量使用不同的名称.这些下划线只是从$注射器的一点帮助,没有痛苦.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读