mercredi 18 février 2015

JavaScript unit test: Cannot check if jQuery method was called

I have an Angular application with a very simple directive for now called animate. I am trying to check using Jasmine whether the method slideDown was called. Currently my directive looks like this:


animateDirective



var animate = function () {
return function (scope, element, attrs) {
scope.$watch(attrs.animate, function () {
element.slideDown(200);
})
}
}


app



var app = angular.module("app", []);

app.controller("homeController", homeController);
app.directive("animate", animate);


In my unit test file I have checked that all my references are correct and I've tested in a browser and the directive is being hit. Here is my unit test class so far:


animateDirectiveUnitTests



describe("animateDirective", function () {
var $compile;
var $scope;
var template = "<p animate></p>";
var isolateScope;

function createDirective() {
var directiveElement = angular.element(template);
$compile(directiveElement)($scope);
$scope.$digest();

return directiveElement;
}

beforeEach(function () {
module("app");
inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$scope = _$rootScope_.$new();
});
});

it("should call slideDown", function () {
var element = createDirective();

spyOn(element, "slideDown");

expect(element.slideDown).toHaveBeenCalled();
})
});


The error message I get from the test result is: "Expected spy slideDown to have been called". I've also tried changing my test to refer to the jQuery object like below but I get the same error:



var element = createDirective();

spyOn($.fn, "slideDown");

expect($.fn.slideDown).toHaveBeenCalled();


I'm not sure why this doesn't register as a call. Is it possible to do this? If so I'd appreciate some guidance on getting this to work. Thanks


Aucun commentaire:

Enregistrer un commentaire