Friday 15 June 2012

ajax - Can I call done without passing done into my real implementation? -



ajax - Can I call done without passing done into my real implementation? -

i writing jasmine tests , using mockjax mock out ajax calls. know jasmine requires work work asynchronously dont solution.

i passing in 'done' real implementation , calling within promise on ajax request.

all examples seem using settimeouts "simulations" not exclusively helpful in opinion.

here's code help understand:

jasmine $.mockjax({ url: "*/api/adminbenefits", contenttype: "application/json", type: "post" }); beforeeach(function (done) { vm.benefit.benefitname(expectedbenefitname); vm.addbenefit(done); <---- dirty }); //assert it('should toast success', function (done) { expect(faketoastr.successvalue()).toequal('"' + expectedbenefitname + '"' + " added successfully."); done(); }); implementation model.addbenefit = function(callback) <---- dirty { var value = model.benefit.benefitname(); $.ajax({ url: rootpath + "/api/adminbenefit", datatype: "text", contenttype: "application/json", type: "post", data: ko.tojson(model.benefit) }).done(function(data) { toastr.success("\"" + value + "\"" + " added successfully."); !callback || callback(); <---- dirty }).fail(function() { !callback || callback(); <---- dirty }); }

am going wrong?

just re-iterate want allow jasmine know when mockjax / ajax .done() without passing in done jasmine viewmodel.

i never followed reply after posting reply in github issue opened, code below how typically see implemented in tests. note might want homecoming result source code in callback(), if want assertions on result info (which may idea).

test code:

describe("some functionality", function() { beforeeach(function () { $.mockjax({ url: "*/api/adminbenefits", contenttype: "application/json", type: "post" }); }); it('should toast success', function (done) { vm.benefit.benefitname(expectedbenefitname); vm.addbenefit(function() { expect(faketoastr.successvalue()).toequal('"' + expectedbenefitname + '"' + " added successfully."); // perchance other assertions on result info passed in callback... done(); // tell jasmine you're done async action }); }); });

ajax jasmine karma-jasmine mockjax jasmine2.0

No comments:

Post a Comment