问题描述
我有一些验证逻辑控制器内部发生的情况,我想单元测试这个逻辑的。
I have some validation logic happening inside a controller and I'd like to unit test this logic.
问题是我不知道如何嘲笑的形式控制器会自动注入的范围。
The thing is I don't know how to mock the form controller that is automatically injected into the scope.
任何想法?
推荐答案
据我所知,你可以试试方法2:
AFAIK, you can try 2 approaches:
-
使用
$编译
服务,并与相应的编译模板$范围
(不忘记所有的合作$范围。$适用()
编译后)。咕噜的html2js是一个伟大的工具,以preprocess你的模板,让他们测试执行之前加入到角的$ templateCache。查看该项目的主页在
use the
$compile
service, and compile your template with appropriate$scope
(don't forget co all$scope.$apply()
after compiling). Grunt's html2js is a great tool to preprocess your templates and have them added to angular's $templateCache before test execution. See the project's homepage at https://npmjs.org/package/grunt-html2js
使用 $控制器
服务,并手动注入的FormController
到 $范围
。但你也必须注入均 NgModelControllers
,你通常会在你的模板。
use the $controller
service, and manually inject FormController
to the $scope
. But you would have to also inject all NgModelControllers
that you would normally have in your template.
这篇关于AngularJS:如何嘲笑范围注射用FormController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!