Angularjs 用茉莉花测试角度定向的局部函数
我有一个用本地函数定义的指令,例如:Angularjs 用茉莉花测试角度定向的局部函数,angularjs,jasmine,karma-runner,Angularjs,Jasmine,Karma Runner,我有一个用本地函数定义的指令,例如: angular.module('clientApp') .directive('myDirective', function () { return { scope : { user_data : "=myDirective" } , link : function (scope) { function is_valid_float() { return true; }; sco
angular.module('clientApp')
.directive('myDirective', function () {
return {
scope : { user_data : "=myDirective" } ,
link : function (scope) {
function is_valid_float() { return true; };
scope.function_on_scope = function () { return true; };
}
}
});
我正在尝试为是否有效\u float
describe('Directive: myDirective', function () {
// load the directive's module
beforeEach(module('clientApp'));
var element,scope;
beforeEach(inject(function ($rootScope,$compile) {
scope = $rootScope.$new();
element = angular.element('<div my-directive></div');
element = $compile(element)(scope);
scope.$digest();
}));
describe('Test', function () {
it('Should be defined', function() {
expect(element).toBeDefined();
expect(scope.function_on_scope).toBeDefined();
});
it('Should check float validity', function () {
expect(is_valid_float('123')).toBeTrue();
expect(is_valid_float('123.2.3')).toBeFalse();
});
});
});
描述('Directive:myDirective',函数(){
//加载指令的模块
在每个模块之前(模块('clientApp');
var元素、范围;
beforeach(注入(函数($rootScope,$compile){
scope=$rootScope.$new();
element=angular.element('您不能测试私有函数。您应该检查从“外部”观察到的指令行为。该行为的实现方式与此无关。嗯,此行为是通过粘贴数据来调用的,因此我不知道如何从外部访问它…您碰巧知道为什么我无法获得对正确范围的引用?例如,在本例中,
function\u on\u scope
?因为您的指令声明了一个隔离范围,所以function\u on\u scope
已定义,但不在您要查找的范围内:)我还尝试通过元素从元素中获取范围。scope()
(正如一些答案所建议的那样),我得到了一个不同的范围,但仍然不是我需要的范围。因为元素。scope()
没有为您提供隔离范围。请尝试元素。隔离范围()
。。。