如何在不模仿依赖项的情况下模拟Controller

时间:2017-11-14 05:12:02

标签: angularjs unit-testing

我有一个像这样的HTML文件

 <div class="textoutput">
    <div ng-controller="TestController">
        <div class="textdiv">
         <div ng-repeat="row in contactperson"> 
            <div ng-if="contactperson.name">
                 <input type="text" name="conTitle1" ng-model="contact1.title" ng-pattern="/^[a-zA-Z0-9\_\- ]*$/" ng-maxlength="10" maxlength="10" value="" required>
            </div>
         </div>
        </div>
      </div>
   </div>

我正在尝试模拟Controller以测试HTML而不调用它所具有的依赖项但是它总是显示我未定义Controller

   beforeEach(inject(function ($compile, _$rootScope_, $templateCache, $http, _$controller_) {

        $scope = _$rootScope_;
        $controller = _$controller_;
        var directiveTemplate = null;
        var req = new XMLHttpRequest();
        req.onload = function () {
            directiveTemplate = this.responseText;

        };
        var url1 = "TextOutput.html";
        req.open('get', url1, false);
        req.send();

        var controller=$controller{'TestController'{}),


        $templateCache.put('template.html', directiveTemplate);
        templateHtml = $templateCache.get('template.html');
        formElem = angular.element('<div>' + templateHtml + '</div>');
        htmlform = $compile(formElem)($scope);

    }));

0 个答案:

没有答案