$范围存在于浏览器的调试器,但终端不存在 [英] $scope exists on browser debugger, but does not exist in terminal

查看:123
本文介绍了$范围存在于浏览器的调试器,但终端不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是取决于控制器从虽然Ajax调用的API获取数据的指令。它工作正常。我想用茉莉花来测试它和奇怪的问题是,当我调试code和检查的价值让我们说 $ scope.measurement 则返回true ,但是当我在终端运行它无法找到 $ scope.measurement 并引发错误预期不确定的是真实的。不知道什么可以是问题。我想这个问题可能与一个孤立的范围,但元素不具有的功能 isolateScope()。是否有任何想法可能是什么问题?

下面是控制器:

 使用严格的;
angular.module('对myApp')
  .controller('MeasurementsTimelineCtrl',['$范围,三围,函数($范围,测量){
    $ scope.measurements = NULL;
    VAR用户id = $ scope.currentUser? $ scope.currentUser.id:​​空;    如果(用户id){
      VAR listOfMeasurements = Measurements.users(用户ID);
      listOfMeasurements.then(功能(数据){
        $ scope.measurements =数据;
      });
    }
  });

这是指令:

 使用严格的;angular.module('对myApp')
  .directive('measurementTimeline',函数(){
    返回{
      限制:'E',
      templateUrl:myView.html',
      控制器:'MeasurementsTimelineCtrl',
      链接:功能(范围,元素){
        范围。在('测量更新',函数(测量$){
          _.defer(函数(){
            如果(测量){
              scope.measurementScroll = TRUE;
            }
          });
        });
      }
    };
  }]);

这是测试:

 使用严格的;    描述(指令:measurementTimeline',函数(){      变量$ rootScope,$编译,元素,范围,createController,$ httpBackend,apiUrl;      beforeEach(函数(){
        模块('对myApp');        注(函数($喷油器){
          $ rootScope = $ injector.get('$ rootScope');
          $编译= $ injector.get('$编译');
          $ httpBackend = $ injector.get('$ httpBackend');
          apiUrl = $ injector.get('apiUrl');
        });
        范围= $ rootScope $新的()。
        元素= angular.element('<仪表测量-时间表>< /仪表测量-时间表>');
        元素= $编译(元)(范围);        范围$摘要()。
        scope.measurements = {[ID:'someId',time_of_test:周二,二零一四年十二月三十零日14:00:00 -0000},
          {ID:'someId',time_of_test:星期五,2014年6月13日14:00:00 -0000'}]        范围$广播(三围更新',scope.measurements)。
        。范围$适用();
      });      描述(PUser',函数(){
        beforeEach(函数(){
          scope.currentUser = NULL;
        });        它(应该......,函数(){
          期待(scope.measurementScroll).toBe(真);
        });
      });
    });


解决方案

修改
看来,波纹管解决方案是一个完美的错案。然而,测试通过,他们不会下降,即使他们是错的。

错解
该测试改变以下后通过:

 它(应该......,函数(){
    范围。$ evalAsync(函数(){
      期待(scope.measurementScroll).toBe(真);
    });
  });

更新:正确的解决方案

有关这个问题的正确解决方案是通过@Michal Charemza在<一个解决href=\"http://stackoverflow.com/questions/27921918/how-to-test-defer-using-jasmine-angularjs/27938040#27938040\">this问题 -
<一href=\"http://stackoverflow.com/questions/27921918/how-to-test-defer-using-jasmine-angularjs/27938040#27938040\">How用茉莉花来测试_.defer(),AngularJs

I have a directive that is depended on a controller which gets data from an api though Ajax call. It works correctly. I am trying to test it using jasmine and the strange issue is that when I debug the code and check for a value of let's say $scope.measurement it returns true, but when I run in the terminal it can't find $scope.measurement and raises an error Expected undefined to be true. no clue what can be the issue. I thought the problem might be with an isolated scope, but the element doesn't have a function isolateScope(). Is there any idea what can be the problem?

Here is the controller:

'use strict';
angular.module('myApp')
  .controller('MeasurementsTimelineCtrl', ['$scope', 'Measurements', function($scope, Measurements) {
    $scope.measurements = null;
    var userId = $scope.currentUser ? $scope.currentUser.id : null;

    if (userId) {
      var listOfMeasurements = Measurements.users(userId);
      listOfMeasurements.then(function(data){
        $scope.measurements = data;
      });
    }
  });

This is the directive:

'use strict';

angular.module('myApp')
  .directive('measurementTimeline', function() {
    return {
      restrict: 'E',
      templateUrl: 'myView.html',
      controller: 'MeasurementsTimelineCtrl',
      link: function(scope, element){
        scope.$on('measurements-updated', function(measurements) {
          _.defer(function(){
            if(measurements) {
              scope.measurementScroll = true;
            }
          });
        });
      }
    };
  }]);

And this is the test:

   'use strict';

    describe('Directive: measurementTimeline', function () {

      var $rootScope, $compile, element, scope, createController, $httpBackend, apiUrl;

      beforeEach(function() {
        module('myApp');

        inject(function($injector) {
          $rootScope = $injector.get('$rootScope');
          $compile = $injector.get('$compile');
          $httpBackend = $injector.get('$httpBackend');
          apiUrl = $injector.get('apiUrl');
        });
        scope = $rootScope.$new();
        element = angular.element('<dashboard-measurement-timeline></dashboard-measurement-timeline>');
        element = $compile(element)(scope);

        scope.$digest();
        scope.measurements = [{id: 'someId', time_of_test: 'Tue, 30 Dec 2014 14:00:00 -0000'},
          {id: 'someId', time_of_test: 'Fri, 13 Jun 2014 14:00:00 -0000'}];

        scope.$broadcast('measurements-updated', scope.measurements);
        scope.$apply();
      });

      describe('PUser', function(){
        beforeEach(function(){
          scope.currentUser = null;
        });

        it('should ......', function () {
          expect(scope.measurementScroll).toBe(true);
        });
      });
    });

解决方案

Edit Seems that the bellow solution is a "Perfect wrong case". However the tests passes they never fall, even if they are wrong.

Wrong solution The test passed after altering the following:

it('should ......', function () {
    scope.$evalAsync(function() {
      expect(scope.measurementScroll).toBe(true);
    });
  });

Update: Right Solution

The right solution for this problem was solved by @Michal Charemza in this question - How to test _.defer() using Jasmine, AngularJs

这篇关于$范围存在于浏览器的调试器,但终端不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆