Javascript 如何在Jasmine测试中加载和编译AngularJs HTML片段/部分?

Javascript 如何在Jasmine测试中加载和编译AngularJs HTML片段/部分?,javascript,angularjs,jasmine,karma-runner,Javascript,Angularjs,Jasmine,Karma Runner,我知道您可以加载、编译和测试角度指令,如下所示: it('Replaces the element with the appropriate content', function () { var element = $compile("<my-directive></my-directive>")($rootScope); $rootScope.$digest(); expect(element).toContain("some HTML..

我知道您可以加载、编译和测试角度指令,如下所示:

it('Replaces the element with the appropriate content', function () {

    var element = $compile("<my-directive></my-directive>")($rootScope);
    $rootScope.$digest();

    expect(element).toContain("some HTML...");
});

我认为您不应该只测试指令的模板,因为指令是代码中使用的,而不是模板本身

然而,我认为您应该执行
$http.get('my-table.html')
,将内容放入变量并编译它

编辑:

下面是一些代码:

var template;

beforeEach(inject(function($http, $httpBackend){
    $http.get('my-table.html').then(function(data) {
        template = data;
    });

    $httpBackend.flush();
}));

it('Replaces the element with the appropriate content', function () {

    var element = $compile(template)($rootScope);
    $rootScope.$digest();

    expect(element).toContain("some HTML...");
});

这只是它应该如何工作的一个想法。不确定这是否真的有效-还没有测试过,但如果我需要的话,我会这样做。

是的,我知道,但是如果你只有一个控制器和一个中继器,为什么要费心把它包装成一个指令?它只是没有增加任何价值-我只是想确保在我的部分中生成的HTML是正确的-这有意义吗?你所说的
$http.get
-这是一个角度服务吗?你能给我一个代码示例吗?谢谢您可以尝试使用stackoverflow链接
it('Replaces the element with the appropriate content', function () {

    var element = $compile("my-table.html")($rootScope);
    $rootScope.$digest();

    expect(element).toContain("some HTML...");
});
var template;

beforeEach(inject(function($http, $httpBackend){
    $http.get('my-table.html').then(function(data) {
        template = data;
    });

    $httpBackend.flush();
}));

it('Replaces the element with the appropriate content', function () {

    var element = $compile(template)($rootScope);
    $rootScope.$digest();

    expect(element).toContain("some HTML...");
});