我正在尝试对 Angular Directive(指令)进行单元测试。这是代码:
describe('Test', function () {
var element,
scope;
// load the service's module
beforeEach(module('app'));
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope.$new();
element = angular.element('<div id="wow"></div>');
$compile(element)(scope);
element.scope().$apply();
}));
it('should', function () {
console.log(document.getElementById('wow'));
});
});
我在控制台中得到null
。
当然这只是一个测试用例。在实际代码中, getElementById
由插件使用,该插件用我的指令包装,这是我正在尝试测试的。
所以我的问题是:为什么getElementById
找不到刚刚编译的DOM元素。如果这是正确的行为,我怎样才能避免这个错误。
谢谢
最佳答案
关于javascript - Angular.js UNIT 测试指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23502265/