【发布时间】:2014-04-03 16:16:49
【问题描述】:
我是 Angular js 测试的新手。我们正在使用 Karma/Jasmine 进行测试。我正在尝试对指令进行单元测试。本质上,我想确保一旦全部编译,该元素现在包含'id =“tf-'......我已经使用转储(元素)进行了验证;事实上我得到了结果,但我的测试不是测试它应该如何。
在我的测试文件中我这样做:
var $scope,element;
beforeEach(inject(function($rootScope,$compile) {
$scope = $rootScope.$new();
element = engular.element('<div tf-swf></div');
$compile(element)($scope);
$scope.$digest();
dump(element);
}));
describe('tfSwf directive' , function() {
it('should place element into DOM' , function() {
expect(element).toContain('<div id="tf');
});
});
所以这就是问题所在......当我运行它时它失败了,我得到一个错误:
TypeError: Object [object Object] has no method 'indexOf' at null.<anonymous>
我知道我转储那个元素看起来很奇怪,但这是输出:
Object{0: <div tf-swf=""><div id="tf-2403-place"></div></div>, length: 1}
内部 div 存在并且具有包含随机数的 id 的事实正是我所追求的……我只是不知道如何在我的测试中找到该内部 div。
【问题讨论】:
标签: angularjs unit-testing jasmine karma-runner