jeudi 29 janvier 2015

Unit testing beginner method

I am currently working on a project that requires us to write unit tests. This is an area where my knowledge is limited. After researching over the last few days I have seen multiple examples showing how to write basic tests that assert if a string is equal to another string.


However, what I am still unclear on is how to approach unit testing as a whole. I have an example method that I need to test below:



public static SearchResults GetSearchResults(SearchFormModel searchForm, int currentItemCount, int skip)
{
var results = new SearchResults();
var client = new RestClient(Settings.Default.SearchWebServiceUrl);
try
{
var request = new RestRequest("{0}", Method.GET);
if (!searchForm.FirstName.IsNullOrEmpty()){request.AddParameter("forenames",searchForm.FirstName);}
if (!searchForm.LastName.IsNullOrEmpty()) { request.AddParameter("surname", searchForm.LastName); }

request.AddUrlSegment("0", "Basic");

request.AddHeader("Accept", "application/json");

var response = client.Execute<SearchResult>(request);
if (response.ResponseStatus != ResponseStatus.Completed)
{
if (response.ErrorException != null)
{
SendErrorEmail(response.ErrorException.Message);
}
throw new Exception(Settings.Default.SearchGenericError);
}

if (response.Data != null)
{
results.Valid = response.Data.Valid;
var error = response.Data.Error;
if (!string.IsNullOrEmpty(error)){error = Settings.Default.SearchGenericError;}
results.Error = error;
if (response.Data.SearchResults != null)
{
results.SearchResults = new List<SearchResult>(response.Data.SearchResults );
}
}
else
{
throw new Exception(Settings.Default.SearchGenericError);
}
}
catch (Exception ex)
{
registrantList.Error = ex.Message;
}
return results;
}


Would a good approach here be two break this down into two separate methods? One that sets the request and gets the response like so:



public static IRestResponse<SearchResults> GetSearchResults(SearchFormModel searchForm, int currentItemCount, int skip)
{
var results = new SearchResult();
var client = new RestClient(Settings.Default.SearchWebServiceUrl);
try
{
var request = new RestRequest("{0}", Method.GET);
if (!searchForm.FirstName.IsNullOrEmpty()){request.AddParameter("forenames",searchForm.FirstName);}
if (!searchForm.LastName.IsNullOrEmpty()) { request.AddParameter("surname", searchForm.LastName); }

request.AddUrlSegment("0", "Basic");

request.AddHeader("Accept", "application/json");

var response = client.Execute<SearchResult>(request);
return response;
}


Which in turn returns to another method which should be testable ie:



public static SearchResults GetSearchResults(IRestResponse<SearchResults> response)
{
var results = new SearchResult();
if (response.ResponseStatus != ResponseStatus.Completed)
{
if (response.ErrorException != null)
{
SendErrorEmail(response.ErrorException.Message);
}
throw new Exception(Settings.Default.SearchGenericError);
}

if (response.Data != null)
{
results.Valid = response.Data.Valid;
var error = response.Data.Error;
if (!string.IsNullOrEmpty(error)){error = Settings.Default.SearchGenericError;}
results.Error = error;
if (response.Data.SearchResults != null)
{
results.SearchResults = new List<SearchResult>(response.Data.SearchResults );
}
}
else
{
throw new Exception(Settings.Default.SearchGenericError);
}
}
catch (Exception ex)
{
registrantList.Error = ex.Message;
}
return results;
}


Apologies for the beginner question, however I want to make sure that this approach is the way to go. Would this be the correct way to approach?


Thanks in advance


Aucun commentaire:

Enregistrer un commentaire