Unit testing 具有依赖项的单元测试
这是我的指令Unit testing 具有依赖项的单元测试,unit-testing,angularjs,jasmine,Unit Testing,Angularjs,Jasmine,这是我的指令 .directive('xDirective', ['x', 'y', function (x, y) { return { restrict: 'CA', link: function (scope, element, attrs, messaging) { //console.log(scope); //console.log(element);
.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
谢谢。我用下面的stackoverflow线程解决了这个问题 代码如下:
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);
}));
});
description('指令:AppVersion',函数(){
在每个模块之前(模块(‘MyApp’);
var元素;
它('should have element text set to config value',inject(函数($rootScope,$compile,x,y){
var scope=$rootScope;
元素=$compile('some Content')(范围);
expect($(element).find('.someClass').css('height')).toBe(config.version);
}));
});
在it块内部,我正在注入与指令相关的“x”和“y”模块
谢谢