angularjs - How do I mock an Angular controller when I compile the directive? -
i have following angular code...
<!-- directive template --> <div style="display:hidden" ng-if="ctrl.test()"> ... // controller ... this.test = function() { return false; }
now need mock ctrl.test
can test dom rendered when true (this isn't real code demo why returns false). try following test...
beforeeach(module('mod')); var $controller, $scope, $httpbackend, $rootscope, $compile; beforeeach(inject(function(_$controller_, _$rootscope_, _$httpbackend_, _$compile_){ $rootscope = _$rootscope_; $scope = _$rootscope_.$new(); $httpbackend = _$httpbackend_; $controller = _$controller_(impactformcontroller, { $scope: $scope }); $compile = _$compile_; })); describe("form functionality", function(){ it("submitting saves form properly", function() { //todo should check response seems uness. var compiled = $compile('<my-d></my-d>')($rootscope); $rootscope.$digest(); var controller = compiled.controller('my-d'); spyon(controller, "test").and.returnvalue(true); $rootscope.$digest(); console.log(json.stringify(controller.test())); console.log(json.stringify(compiled[0].innerhtml)); }); })
the console calling method returns true expected. however, dom never updated. after mock expect this...
<div class="wrapper"> <div style="display:hidden"> </div> <!-- ng-if --> </div>
i tried...
// trying recompile spyon(controller, "test").and.returnvalue(true); $rootscope.$digest(); var recompiled = $compile(elem)($rootscope); console.log(json.stringify(recompiled[0].innerhtml));
still nothing.
Comments
Post a Comment