在指令

时间:2015-05-17 07:30:01

标签: angularjs unit-testing karma-jasmine

我有一个指令,在做某事之前检查表单的有效性。它正在工作,但我的单元测试仍然失败,因为我不能在我的业力测试中模拟formController。

我有以下指令:

.directive('directiveName', function() {
    return {
        restrict: 'A',
        require: 'form',
        link: function(scope, element, attrs, ctrl) {
            element.bind('submit', function(event) {
                if(!ctrl.$valid) ...do something here...
            }
        }
    };
});

这是我的测试初始化​​:

var form, element, $scope; 

beforeEach(inject(function($rootScope, $compile, $q) {
    $scope = $rootScope.$new();
    var template = angular.element('<form fl-submit="mockFunction()">' +
                          '<button type="submit"> </button> </form>');

    $scope.mockFunction = function() {
      return promise.promise;
    };

    element = $compile(template)($scope);
    form = $scope.form;
}));

describe('and the form is not valid', function() {
    form.$valid = false;
    ...expect something here...
});

非常感谢您的帮助!

0 个答案:

没有答案