gpt4 book ai didi

javascript - $scope 存在于浏览器调试器中,但不存在于终端中

转载 作者:行者123 更新时间:2023-12-03 11:04:58 24 4
gpt4 key购买 nike

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

这是 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;
});
}
});

这是指令:

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

最佳答案

怎么样?

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

更新:

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

spyOn(obj, '方法').andCallThrough()

关于javascript - $scope 存在于浏览器调试器中,但不存在于终端中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com