1

这是我的指令

.directive('xDirective', ['x', 'y', function (x, y) {
        return {
            restrict: 'CA',
            link: function (scope, element, attrs, messaging) {
                //console.log(scope);
                //console.log(element);
                //console.log(attrs);

                if (x.isResponsive && x.responsiveMode === y.responsive.mode.phone) {

                    //set the height of the header based on the device height
                    var offset = (attrs.heightOffset ? attrs.heightOffset : 0);                    
                    element.css("height", (x.device.height - offset) + 60 + "px");
                }
            }
        };
    }])

其中 x 和 y 是指令的依赖项..我想对该指令进行单元测试..我如何继续使用 jasmine。

谢谢。

4

1 回答 1

3

我使用下面的stackoverflow线程弄清楚了

使用依赖项测试 angularjs 指令

下面是代码:

describe('Directive: AppVersion', function () {
    beforeEach(module('MyApp'));

    var element;

    it('should have element text set to config value', inject(function ($rootScope, $compile, x,y) {
        var scope = $rootScope;
        element = $compile('<div class="someClass" xDirective >some Content</div>')(scope);
        expect($(element).find('.someClass').css('height')).toBe(config.version);
    }));
});

在 it 块内,我正在注入依赖于指令的“x”和“y”模块

谢谢

于 2013-08-06T07:08:27.780 回答