如何在指令的链接中单元测试函数?我试过这样的事,但没有奏效:
指示:
app.directive("hello", function(){
return {
link: function(scope){
scope.hello = function(){
return "hello";
};
}
};
});
单元测试:
describe("Hello directive", function(){
var compile, scope;
beforeEach(inject(function($compile, $rootScope){
scope = $rootScope.$new();
compile = $compile;
}));
it("should return hello", function(){
var element = compile("<hello></hello>")(scope);
expect(element.scope.hello()).toBe("hello");
});
});
最佳答案 您缺少对模块的调用.
指令限制看起来很好(现在),因为默认值是根据angular source for $compileProvider的’EA’.
Note: Said default was introduced in angular-1.3.0(07001, 07002), prior to that version it was simply
'A'
. Which would have been an issue in your case, seeing as how your question was posted in May’14.
我设置了一个小提示,通过两个更改来展示您的实现;
>调用模块,以便加载指令定义.
>将角度版本归为1.3.0.
是的,我迟到了,但你的问题仍然是[angularjs] [单元测试]中最难回答的问题之一.我想我会改变它.