c# - Unit Testing void returning method -


i using moq first time , not sure on correct approach testing void returning method have. have read post helpful didnt include many snippets of code guidance. method updates db responses external webservice list of car objects

my method below:

    public void updatedbwithwebresponse(response response, list<car> cars)     {         try         {             if (response == null)             {         //exception logged             }             foreach (var webresponse in response.responses)             {                 var car = cars.first(c => c.id == webresponse.id);                  if (response.detailresponse == null || response.detailresponse.values == null)                 {                    //exception logged                 }                  foreach (var detailresponse in response.hazardresponse.values)                 {                     updatedetailresponseoncar(detailresponse, car);                 }                              }     //update db             _carrepository.update(cars);          }         catch (exception ex)         {            //log error         }     } 

so takes 2 parameters web response object , list of car objects - updatedetailresponseoncar(detailresponse, car); private method maps web reponse car object , data saved db.

i guess want test if response null exception called? similariy inner detail response if null exception thrown. , if create mock response object , mock list of cars want save test instance of db , assert correct values mapped?

does seem strategy test above method , has got code snippets testing null response throws exception , other cases?

first, remove catch around everything.

ideally throw exceptions absolutely necessary, , allow calling code catch them.

that way can catch exception in test.

perhaps best use specific exceptions, e.g. argumentnullexception case when response null.

see following mstest: http://www.contentedcoder.com/2012/01/asserting-exceptions-in-mstest-with.html


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -