I have an Angular directive which sets the value of some $scope property based on the value of an injected constant. I want to test that this value is being correctly initialized from the constant, so I would like to change the constant's value within an individual it block. (Preferably within one, but changing the value between multiple blocks would be OK too)
Is this possible, and how can I do it?
simplified example:
//////// directive ////////
angular.module('myApp.directives', [])
.constant('THE_CONSTANT', 'hello world')
.directive('myDirective', ['THE_CONSTANT', function (THE_CONSTANT) {
return {
restrict: 'E',
link: function ($scope) {
$scope.propertyBasedOnConstant = THE_CONSTANT;
}
};
}]);
//////// test ////////
describe('myDirective', function () {
var $element, $scope;
beforeEach(module('myApp.directives'));
beforeEach(module(function ($provide) {
$provide.constant('THE_CONSTANT', 'foo');
}));
beforeEach(inject(function ($rootScope, $compile) {
$scope = $rootScope;
$element = angular.element('<my-directive></my-directive>');
$compile($element)($scope);
$scope.$digest();
}));
afterEach(function () {
$scope.$destroy();
$element.remove();
});
it("should correctly reflect the constant's value", function() {
expect( $scope.propertyBasedOnConstant ).to.equal('foo');
// now I want to change the constant's value and re-initialize the directive
});
});
Aucun commentaire:
Enregistrer un commentaire