我将如何测试一个$ scope.watch在茉莉花(AngularJS)的变化? [英] How would I test a $scope.watch (AngularJS) change in Jasmine?

查看:212
本文介绍了我将如何测试一个$ scope.watch在茉莉花(AngularJS)的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚开始写与AngularJS的东西,我不知道怎么去写一个测试为这个特别的事情。我建立了具有不同状态的帮助请求模式。所以在我的控制,我使用$ scope.request_mode变量。不同的链接激活的帮助请求变量设置为不同的东西。

I've just recently started writing something with AngularJS and I'm not sure how to go about writing a test for this particular thing. I'm building a "Help Request" mode that has different states. So in my controller, I use a $scope.request_mode variable. The different links to activate help requests set that variable to something differently.

然后我的指令里面,我在做一个 $ $范围手表('request_mode',函数(){...}); 选择性地激活或取消事情请求模式的变化。在code所有的伟大工程,但我遇到的问题是测试。我似乎无法得到茉莉花拿起 $范围。$看,实际上任何火灾时它的变化。

Then inside my directive, I'm doing a $scope.$watch('request_mode', function(){...}); to selectively activate or deactivate things as the request mode changes. The code all works great, but the problem I'm having is with testing. I cannot seem to get Jasmine to pick up the $scope.$watch and actually fire anything when it changes.

我敢肯定有人以前碰到这个,所以任何建议将是非常美联社preciated。

I'm sure someone has run into this before, so any suggestions would be very much appreciated.

推荐答案

在你的单元测试,你需要的手动拨打 $范围。$摘要() $范围。$适用()引发 $范围。$表()

In your unit tests you need to manually call $scope.$digest() or $scope.$apply() to trigger $scope.$watch().

通常在code你就不必这样做,因为像 NG-点击指令不要 $ rootScope。$申请为你在幕后。

Normally in your code you wouldn't have to do this, since directives like ng-click do $rootScope.$apply for you behind the scenes.

这篇关于我将如何测试一个$ scope.watch在茉莉花(AngularJS)的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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