Angularjs 如何在jasmine中测试自定义指令

Angularjs 如何在jasmine中测试自定义指令,angularjs,jasmine,Angularjs,Jasmine,我正试图用Jasmine为自定义指令编写测试用例。目前,我有一个指令,如下所示: 这是我在fiddler中的代码 }) 我的茉莉花密码在fiddler中 我尝试了不同的方法来测试$watch函数无法实现它,请帮助您在scope.trigger上设置了$watch,但它在focus me中传递 您可以使用spyOn访问元素[0]。聚焦并为聚焦me=true和聚焦me=false创建测试: 你试了什么?如果你表现出一些努力,然后问一个关于某个特定问题的问题,这是非常感谢的。你能检查一下这篇文章吗

我正试图用Jasmine为自定义指令编写测试用例。目前,我有一个指令,如下所示: 这是我在fiddler中的代码

})

我的茉莉花密码在fiddler中 我尝试了不同的方法来测试$watch函数无法实现它,请帮助

您在scope.trigger上设置了$watch,但它在focus me中传递

您可以使用spyOn访问元素[0]。聚焦并为聚焦me=true和聚焦me=false创建测试:


你试了什么?如果你表现出一些努力,然后问一个关于某个特定问题的问题,这是非常感谢的。你能检查一下这篇文章吗
app.directive('focusMe', function ($timeout) {
 return {
    scope: { trigger: '=focusMe' },
    link: function (scope, element) {
        scope.$watch('trigger', function (value) {
            if (value === true) {
                element[0].focus();

            }
        });
    }
};
describe('Directive: focusMe', function() {
    var element,
        scope;

    beforeEach(module('CommonBusInfo'));
    beforeEach(inject(function($rootScope) {
        scope = $rootScope.$new();
    }));

    it("should focus if focus-me is true", inject(function($compile) {
        element = angular.element('<input type="text" focus-me="true" value="chaitu">');
        element = $compile(element)(scope);
        spyOn(element[0], 'focus');

        scope.$digest();

        expect(element).toBeDefined();
        expect(element[0].focus).toHaveBeenCalled();
    }));

    it("should NOT focus if focus-me is falsy", inject(function($compile) {
        element = angular.element('<input type="text" focus-me="" value="chaitu">');
        element = $compile(element)(scope);
        spyOn(element[0], 'focus');

        scope.$digest();

        expect(element).toBeDefined();
        expect(element[0].focus).not.toHaveBeenCalled();
    }));
});