How to unit test angularjs form?

前端 未结 4 1612
长情又很酷
长情又很酷 2020-12-13 10:41

I have been learning AngularJS and things have been going pretty smoothly regarding unit testing, but I\'ve reached a bit of a tricky spot.

Suppose I have a simple f

4条回答
  •  难免孤独
    2020-12-13 11:04

    Here's a way to unit test with an angular form without having to compile a controller's template. Works well for me in my limited usage.

    describe('Test', function() {
    
      var $scope, fooController;
    
      beforeEach(function($rootScope, $controller, formDirective) {
    
        $scope = $rootScope.$new();
        fooController = $controller('fooController', {$scope: $scope});
    
        // we manually create the form controller
        fooController.form = $controller(formDirective[0].controller, {
          $scope: $scope,
          $element: angular.element("
    "), $attrs: {} }); }); it('should test something', function() { expect(fooController.form.$valid).toBeFalsy(); }); });

提交回复
热议问题