I would have the test written as below. Demo PLUNKER
describe('Directive: paypalButton', function () {
var element, scope, $compile;
beforeEach(function(){
module('myApp');
inject(function ($rootScope, $compile) {
scope = $rootScope;
element = angular.element('<my-directive></my-directive>');
$compile(element)(scope);
});
});
it('replaced content should be abc', function() {
element.scope().$digest();
expect(element.text()).toEqual(' ... abc ... ');
});
});
with this change in directive:
app.directive('myDirective', function() {
return {
restrict: 'E',
scope: {},
compile: function(element) {
element.append('<form> ... abc ... </form>');
}
};
});
Note:-
- I think while testing it does not like to replace the element altogether because directives scope is bound to that element. You can try
append()
on element. - Also, since you are using isolated scope in directive, digest cycle has to be initiated in the isolated scope. Therefore,
element.scope().$digest();
.