我想测试一个指令,其中通过我的一段代码观察一个属性:
function linkFn(scope, element, attrs) {
attrs.$observe('myattr', function() {
console.log('do something');
我有一个测试,这是否:
var html = '<my-directive></my-directive>';
var scope;
var element;
beforeEach(module('app', function($provide) {}));
beforeEach(inject(function($injector, $compile, $rootScope) {
scope = $rootScope.$new();
element = $compile(angular.element(html))(scope);
}));
describe('after being compiled', function() {
it('my test', function() {
element[0].setAttribute('myattr', 'hello');
console.log(element); // shows that myattr is set properly
scope.$digest();
// the console.log is not triggered, why?
});
不幸的是,这是行不通的。我的意思是做美元消化这可以称为美元无论如何,但仍然尝试美元适用于自己,没有运气。 –
我编辑了我的答案 – Maarten
仍然不起作用。唯一的输出是来自初始页面加载,而不是来自单元测试。 –