当指令是属性时,单元测试失败

厄姆·珀西(Oam Psy)

我有一个声明为Attribute的指令:

app.directive('myDirective', function() {
    return {
        restrict: 'A',
        replace: true,
        transclude: true,
        scope: {
            data: "="
        },
        template:
            '<p class="my-paragrapgh">' +
                '<label>Hello</label>' +
            '</p>'
    }
});

我有一个单元测试,但失败了:

describe('myDirective test', function () {
var scope, compile, element;

beforeEach(module('myModule'));

beforeEach(inject(function ($rootScope, $compile) {
    scope = $rootScope.$new();

    element = angular.element("<div my-directive></div>");
    $compile(element);
    scope.$digest();
}));

it('should have a my-paragrapgh class', function () {
    expect($(element).find('p')[0]).toHaveClass('my-paragrapgh');
});

});

但是,如果我将求助对象转换为Element,并删除replace和transclude:

app.directive('myDirective', function() {
    return {
        restrict: 'E',
        //replace: true,
        //transclude: true,
        scope: {
            data: "="
        },
        template:
            '<p class="my-paragrapgh">' +
                '<label>Hello</label>' +
            '</p>'
    }
});

我的单元测试通过了:

describe('myDirective test', function () {
var scope, compile, element;

beforeEach(module('myModule'));

beforeEach(inject(function ($rootScope, $compile) {
    scope = $rootScope.$new();

    element = angular.element("<my-directive></my-directive>");
    $compile(element);
    scope.$digest();
}));

it('should have a my-paragrapgh class', function () {
    expect($(element).find('p')[0]).toHaveClass('my-paragrapgh');
});

});

如何成功测试声明为属性的指令?我正在使用Karma,Jasmine和PhantomJS

马特·赫伯斯特里特(Matt Herbstritt)

ng-transclude当您具有时,您将需要在模板中某个位置,transclude: true以便Angular知道将HTML注入的位置。尝试:

app.directive('myDirective', function() {
    return {
        restrict: 'A',
        replace: true,
        transclude: true,

        scope: {
            data: "="
        },

        template:
            '<div ng-transclude><p class="my-paragrapgh">' +
                '<label>Hello</label>' +
            '</p></div>'
    }
});

更新

看起来replace这可能是导致此问题选项。

app.directive('myDirective', function() {
    return {
        restrict: 'A',

        scope: {
            data: "="
        },

        template:
            '<p class="my-paragrapgh">' +
                '<label>Hello</label>' +
            '</p>'
    }
});

随着replace: true您的内在HTML是:

失败

<label>Hello</label>

使用replaceundefined,您可以

经过

<p class="my-paragrapgh"><label>Hello</label></p>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当单元测试AngularJS指令时,urlIsSameOrigin函数失败

来自分类Dev

当单元测试AngularJS指令时,urlIsSameOrigin函数失败

来自分类Dev

单元测试 Angular 属性指令

来自分类Dev

单元测试在应该失败时通过

来自分类Dev

单元测试失败

来自分类Dev

当Maven单元测试失败时,如何使Jenkins构建失败?

来自分类Dev

单元测试属性

来自分类Dev

Laravel单元测试因自定义属性而失败

来自分类Dev

因果报应-单元测试Angular JS指令时div的ui属性不正确

来自分类Dev

将数据传递到指令进行测试时,Angular指令单元测试问题

来自分类Dev

结果相等时,Python单元测试失败

来自分类Dev

启动时调用函数时单元测试失败

来自分类Dev

监视服务方法时,Jasmine单元测试失败

来自分类Dev

导入头文件时单元测试失败

来自分类Dev

启动时调用函数时单元测试失败

来自分类Dev

监视服务方法时,Jasmine单元测试失败

来自分类Dev

为什么重构时我的单元测试失败

来自分类Dev

Angular 单元测试在使用(提交)时失败

来自分类Dev

使用 rxjs switchmap 时,angular 5 单元测试失败

来自分类Dev

Observable 单元测试在应该失败时通过

来自分类Dev

Umbraco单元测试失败

来自分类Dev

验证单元测试失败

来自分类Dev

“单元测试失败”的BeautifulSoup

来自分类Dev

Umbraco单元测试失败

来自分类Dev

Rails单元测试失败

来自分类Dev

尽管单元测试确实通过了,但由于单元测试失败,所以运行了属性测试

来自分类Dev

单元测试IBOutlet的属性

来自分类Dev

具有外部依赖关系的Jasmine单元测试指令因TypeError:'[object Object]'而失败

来自分类Dev

MutationObserver 上的 Angular 指令单元测试失败:参数 1 不是“节点”类型

Related 相关文章

热门标签

归档