要熟悉指令測試,我創建了下面顯示的簡單示例。不幸的是,測試失敗了,似乎鏈接功能從未被調用。該指令在應用程序中使用時可以工作。未在測試中鏈接的角度指令
我試過硬編碼message
屬性,刪除鏈接函數中的條件,甚至從$ watch中提取attr集合,但測試仍然失敗。
還有其他類似的帖子,其原因是由於缺少一個$摘要的電話,但我確實有這個,我已經嘗試將其移入it
規範塊。
如果我跑console.log(elem[0].otherHTML)
通話範圍結合似乎工作
<wd-alert type="notice" message="O'Doole Rulz" class="ng-scope"></wd-alert>
我缺少什麼?
alert.spec.js
"use strict";
describe('Alert Specs', function() {
var scope, elem;
beforeEach(module('myapp'));
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope;
scope.msg = "O'Doole Rulz";
elem = angular.element('<wd-alert type="notice" message="{{msg}}"></wd-alert>');
$compile(elem)(scope);
scope.$digest();
}));
it('shows the message', function() {
expect(elem.text()).toContain("O'Doole Rulz");
});
});
alert.js
angular.module('myapp').directive('wdAlert', function() {
return {
restrict: 'EA',
replace: true,
template: '<div></div>',
link: function(scope, element, attrs) {
attrs.$observe('message', function() {
if (attrs.message) {
element.text(attrs.message);
element.addClass(attrs.type);
}
})
}
}
});
而不是'scope = $ rootScope',你試過了嗎?scope = $ rootScope。$ new()'?後者是我記得在文檔中看到的模式。 – cmw
它應該只是$ rootScope的指令。我剛剛發現了這個問題,這是一個業力包含問題 - >使用'* .js'而不是'**/*。js'。對我不好。 – chris