Karma在测试中编译Angular指令

时间:2014-03-10 16:38:07

标签: angularjs karma-runner

您好我正试图在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类,并且不编译完整的指令。

1 个答案:

答案 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);
    });

});