unit testing - Faking out FormData append with Jasmine -
i need test unit of code constructs formdata object, appends file , submits ajax request server. issue don't have file object provide formdata append method call. wanted spyon append method , call fake function of own. possible? if not, how can test code.
the following unit of code want test:
var fd = new formdata(); // listoffiles array of files , filename string var file = _.findwhere(listoffiles, { name: filename }); fd.append("file", file, filename); $.ajax(/* options */); the following have unit test, doesn't job:
it("does something", function() { var mockfiles = [{ name: "test file.pdf" }]; // adddocs call adds files listoffiles array above someview.adddocs(mockfiles); var ajaxspy = spyon($, "ajax"); // doesn't work spyon(formdata.prototype, "append").andreturn(false); // event causes above unit of code run app.trigger('someevent'); // more stuff... }); right following error:
failed execute 'append' on 'formdata': no function found matched signature provided which happens because spy doesn't called.
any appreciated how spy on formdata methods.
turns out wrong , following code spy correctly on formdata.append method:
spyon(formdata.prototype, "append"); the issue put far nested in describe blocks other tests failing because tried call formdata.append , no spy set tests.
while working through solution, realized following concept work since don't care uploading file:
spyon(window, 'formdata').andreturn({ "append": jasmine.createspy() }); hopefully can needs spy on formdata!
Comments
Post a Comment