我的<custom-directive>有replace:true和template: '<img />'。我该如何为它编写单元测试呢?我想我想测试一下它是否真的用img代替了自定义指令。
it('should be transformed to <img>', function(){
var elm = $compile('<custom-directive></custom-directive>')(scope);
scope.$digest();
var t = elm.find('img'); // wrong! it replaces the element. it won't find another one inside
//expect(elm).toBeAnImgElement ?
});我找不到合适的匹配者。我见过的最接近的情况是检查内容(elm.html()或elm.text()),但我的标记是空的。
发布于 2013-05-04 14:26:39
将指令包装在div中,如下所示:
describe('Directive: custom', function () {
beforeEach(module('App'));
var element, $scope;
it('should be transformed to <img>', inject(function ($rootScope, $compile) {
$scope = $rootScope.$new();
element = angular.element('<div><custom-directive></custom-directive></div>');
element = $compile(element)($scope);
expect(element.children('img').length).toBe(1);
}));
});发布于 2013-05-04 02:01:28
您可以获取实际的HTMLElement对象并检查其标记名。使用elm[0]获取实际的HTMLElement
expect(elm[0].tagName).toEqual('A');https://stackoverflow.com/questions/16362681
复制相似问题