【问题标题】:$scope exists on browser debugger, but does not exist in terminal$scope 存在于浏览器调试器中,但不存在于终端中
【发布时间】:2015-01-12 15:11:14
【问题描述】:

我有一个依赖于控制器的指令,该控制器通过 Ajax 调用从 api 获取数据。它工作正常。我正在尝试使用 jasmine 对其进行测试,奇怪的问题是,当我调试代码并检查值时,假设为 $scope.measurement 它返回 true,但是当我在终端中运行时它找不到 $scope.measurement 和引发错误Expected undefined to be true。不知道可能是什么问题。我认为问题可能出在一个孤立的范围内,但element 没有函数isolateScope()。有什么想法可能是什么问题吗?

这里是控制器:

'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;
      });
    }
  });

这是指令:

'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;
            }
          });
        });
      }
    };
  }]);

这是测试:

   '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);
        });
      });
    });

【问题讨论】:

    标签: javascript angularjs unit-testing scope jasmine


    【解决方案1】:

    怎么样?

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

    更新:

    我认为您还需要在 _.defer 上使用 andCallThrough 方法

    spyOn(obj, 'method').andCallThrough()

    【讨论】:

    • 编辑。抱歉 BuriB 我它似乎正在工作,现在又坏了。似乎这个解决方案不起作用:(
    【解决方案2】:

    编辑 似乎下面的解决方案是“完美的错误案例”。然而,测试通过了,它们永远不会失败,即使它们是错误的。

    错误的解决方案 更改以下内容后测试通过:

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

    更新:正确的解决方案

    @Michal Charemza 在this question 中解决了这个问题的正确解决方案 - How to test _.defer() using Jasmine, AngularJs

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 2012-03-10
      • 2013-05-28
      • 2014-06-27
      • 1970-01-01
      相关资源
      最近更新 更多