c# - Unit testing Void method which calls another void, starts Task() -


i'm looking advice on writing unit tests code below. implementation aside (it's not code, i've been tasked retroactively write tests it) suggest how might test this? i'm not using nunit or similar framework; using testing tools built visual studio.

i'm new writing unit tests, imagine should @ least test following:

  • valid response passed saveformbrokerresponse() method
  • test valid exceptions thrown catch()
  • testing started task, not sure how this

i've stripped bit out of function, instantiation , population of objects:

public void saveresponse(iform form, bool islive, httprequestbase request) {     try     {         var response = new formbrokerresponses();         // initialize vars on response          using (var memory = new memorystream())         {             var serializer = new datacontractserializer(typeof(formkeyvalue[]));             serializer.writeobject(memory, request.form.allkeys.select(r => new formkeyvalue(r, request.form[r])).toarray());             memory.flush();             memory.seek(0, seekorigin.begin);             response.values = encoding.utf8.getstring(memory.toarray());         }          _datahandler.saveformbrokerresponses(response);     }     catch (exception ex)     {         throw new exception("boom explosions");     }      task.factory.startnew(() => dispatchformresponseviaemail(form, islive, request.form.allkeys.todictionary(r => r, r => (object)request.form[r]))); } 

i realize testing void implementations tricky , questionable , there integration test concerns here, said can't (currently) change implementation , need write tests have.

if allowed make slight modifications code following small change anyway :

public void saveresponse(iform form, bool islive, httprequestbase request) {     try     {         var response = new formbrokerresponses();         // initialize vars on response          using (var memory = new memorystream())         {             var serializer = new datacontractserializer(typeof(formkeyvalue[]));             serializer.writeobject(memory, request.form.allkeys.select(r => new formkeyvalue(r, request.form[r])).toarray());             memory.flush();             memory.seek(0, seekorigin.begin);             response.values = encoding.utf8.getstring(memory.toarray());         }          _datahandler.saveformbrokerresponses(response);     }     catch (exception ex)     {         throw new exception("boom explosions");     }      dispatch(form,islive,request); }  virtual void dispatch(iform form, bool islive, httprequestbase request){     task.factory.startnew(() => dispatchformresponseviaemail(form, islive, request.form.allkeys.todictionary(r => r, r => (object)request.form[r]))); } 

i don't know class named suppose class named dutclass, can derive different implementation of class following:

public class unittestclass : dutclass{     override dispatch(){         //don't or set state variable method called     } } 

then instead of testing dutclass test unittextclass has different implementation of dispatch method , not start task @ all. can test in fact method called, test exceptions , on.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -