I am using Moq for the first time and not really sure on the correct approach for Testing a void returning method I have. I have read this post which was helpful but didnt include many snippets of code for guidance. My method updates my DB with responses from an external webservice for a list of my car objects
My method is as 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 the DB
_carRepository.Update(cars);
}
catch (Exception ex)
{
//log error
}
}
So it takes two parameters a Web response object and a list of car objects - UpdateDetailResponseOnCar(detailResponse, car);
is a private method which maps the web reponse to the car object and then the data is saved to the DB.
I guess what I want to Test is that if the response is null then an exception is called? Similariy with the inner detail response if null is exception thrown. And then if I create a Mock response object and a mock list of cars I want to save that to a Test instance of my DB and assert that the correct values were mapped?
Does this seem like a good strategy to test the above method and has anyone got any code snippets for testing the null response throws exception and for the other cases?