【发布时间】:2017-04-30 17:05:57
【问题描述】:
我已经编写了指令,该指令在参数中采用指令名称并将该指令动态加载为休闲:
.directive("loadDirective", function($compile, $timeout) {
return {
restrict: 'E',
scope: {
Dtype: '=type',
subType: '@',
data: "=data"
},
link: function(scope, element) {
scope.$watch('Dtype', function() {
$timeout(function() {
var generatedTemplate = '<div ' + scope.Dtype + (scope.subType ? '-' + scope.subType + '-directive' : '-directive') + ' data="data" >dd</div>';
element.empty().append($compile(generatedTemplate)(scope));
})
})
},
};
})
这是我将动态加载的指令之一
.directive("dailyIntervalDirective", function() {
return {
scope: {
data: '='
},
restrict: 'A',
templateUrl: '/flat-ui/tpls/daily-interval.html'
};
})
现在我正在尝试为 loadDiretive 编写测试用例来测试它是否加载指令,如下所示:
describe("loadDirective directive", function() {
var elm, scope;
beforeEach(module('guideApp.directives'));
beforeEach(module('/flat-ui/tpls/daily-interval.html'));
beforeEach(angular.mock.inject(function($rootScope, $compile) {
scope = $rootScope;
elm = angular.element('<load-directive type="directive" sub-type="interval" data="schedulerData"></load-directive>');
compile = $compile;
compile(elm)(scope);
scope.schedulerData = {
interval: 1,
}
scope.$digest();
}));
it("should be able to load daily directive", function() {
scope.directive = "daily";
var intervaldirective = elm.find('div[daily-interval-directive]');
expect(intervaldirective.length).toEqual(1);
});
});
这对我来说不是很好。我试图记录榆树,但它没有加载dailyInterevalDirective。
【问题讨论】:
-
嗨 Rhushikesh,指令“loadDirective”和“dailyIntervalDirective”是否按预期工作??
-
它工作正常吗?但是当我编写测试用例时,现在可以测试它的加载与否
标签: javascript angularjs unit-testing testing angular-directive