您好我正试图在Karma测试中编译一个Angular指令,如下所示:
describe("Midway: Testing My App", function() {
var $compile, $rootScope;
beforeEach(module('KSSApp'));
beforeEach(inject(
['$compile','$rootScope', function($c, $r) {
$compile = $c;
$rootScope = $r;
}]
));
it("should compile the widget", function() {
var element = $c('<My-app></My-app>')($r);
console.log(element);
});
});
然而,这只是向元素添加了ng-scope
类,并且不编译完整的指令。
答案 0 :(得分:4)
你可能需要运行一个摘要循环,而且我认为你已经反转注入和测试变量($scompile
和$rootScope
):
describe("Midway: Testing My App", function() {
var $compile, $rootScope;
beforeEach(module('KSSApp'));
beforeEach(inject(
['$compile','$rootScope', function($c, $r) {
$compile = $c;
$rootScope = $r;
}]
));
it("should compile the widget", function() {
// You're out of the inject function so I used full variable names
var element = $compile('<My-app></My-app>')($rootScope);
// run a digest loop
$rootScope.$digest();
console.log(element);
});
});