I find it very difficult to figure out how to set up an object that allows me to test my jQuery calls. I don't need to make fun of any Async calls or anything else, just basic usage. So let me outline my function that I want to test (truncated for simplicity):
listGamesCallback : function(data) { var gameList = $("#gameList select"); gameList.empty(); $.each(data, function() { var newOption = $('<option>', {value : this.gameId }); newOption.text(string); newOption.data("isJoinable", isJoinable);
I need to make fun of jQuery here to unit test this method, but I cannot figure out how to do this in javascript. Even without jsMockito, I donโt know how to create an object with properties that jQuery has in this situation. Any help with this would be appreciated.
I use jsTestDriver, jsHamcrest, jsMockito and jQuery. However, a generic approach to creating an $ object that has these properties would also be awesome. Thanks!
For those who asked, this is what I came up with, which seemed to be curious. But I do not understand why.
var saved$ = $; var mockContruct = mockFunction(); var mockedGamelist = mock(jQuery); var mockedOption = mock(jQuery); mocked$ = (function() { var test = function(name) { var args = jQuery.makeArray(arguments); return mockContruct.call(test, args); }; $.extend(test, $);
source share