AngularJS $scope.$watch Jasmine 测试中未定义的回调函数参数
AngularJS $scope.$watch callback function parameter undefined in Jasmine test
我在控制器中为名为 ctrl.test
的范围变量定义了一个$scope.$watch
。在生产部署中,$watch的回调函数使用正确的值 (newVal) 调用,但从 Jasmine 测试调用时,使用 undefined
调用。直接访问ctrl.test
确实会产生正确的值。
有人能对此有所了解吗?这里有一个可以摆弄的 Plunk。
使用控制器作为模式时,请使用函数作为监视表达式。
取代:
$scope.$watch('ctrl.test', ...
跟:
$scope.$watch(function () {
return ctrl.test;
}, ...
新普伦克:http://plnkr.co/edit/4tb4nhjtdRtv8cLURWZg?p=preview
有关这种情况原因的更多信息:https://stackoverflow.com/a/25748208/2943490
就我而言,最好的解决方案是像往常一样保持手表
$scope.$watch( 'myController.varToWatch', function(newVal,oldValue) {
//blabla
});
并将控制器作用域附加到属性:$scope.myController = myController;
beforeEach(inject(function(_$controller_, _$httpBackend_,_$rootScope_){
$httpBackend = _$httpBackend_;
$scope = _$rootScope_.$new();
$controller = _$controller_;
myController = $controller('settingsController', { $scope: $scope });
$scope.myController = myController;
}));
灵感来源于这里 : http://juristr.com/blog/2014/11/learning-ng-testing-watch-expressions/
相关文章:
- 要求未定义JS回调参数
- Axios spread()具有未知数量的回调参数
- 节点回调参数无法断言instanceof Error
- each()-将额外的参数传递给回调参数
- AngularJS tokenWrapper-错误的回调参数
- 拉斐尔未运行回调参数
- 使用 jQuery 回调参数通过 AJAX 调用 PHP 函数
- 将函数的执行同步为 node.js 中的回调参数
- 当第一个函数完成没有回调参数时执行第二个函数
- 使用回调参数从函数中获取值
- 如何使用可选的回调参数制作 javascript 函数
- 为什么设置超时's回调参数可以接受参数
- 对 Array.prototype.some 使用额外的回调参数
- 使用Google闭包编译器记录回调参数
- JavaScript Object Literal“;这个“;关键字jQuery回调参数传递
- 在Meteor中运行的回调中未定义回调参数
- 回调参数名称
- 将jQueryajax回调参数定义为现有对象类型
- Reactjs路由器匹配回调参数总是未定义的
- Javascript自定义事件更改回调参数