mercredi 8 avril 2015

How to unit-test this method?

I have a method and it is also the method that I am trying to write a test for:



public IPlayerResponse GetPlayerStats(IPlayerRequest playerRequest)
{
string playerId = playerRequest.PlayerId;

string requestUri = playerId + "/evaluate";

var request = new HttpRequestMessage(HttpMethod.Post, requestUri)
{
Content = new StringContent
(JsonConvert.SerializeObject(playerRequest),
System.Text.Encoding.UTF8,
"application/json")
};

var response = _client
.SendAsync(request, CancellationToken.None)
.Result;
var responseContent = response.
Content
.ReadAsStringAsync()
.Result;



var playerResponseModel =
JsonConvert.DeserializeObject<PlayerResponseModel>(responseContent);
var playerResponse = _mapper
.Map<IPlayerResponse>(playerResponseModel);

return playerResponse;
}


The request interface:



public interface IPlayerRequest
{
IEnumerable<IPlayerStatsItem> Items { get; set; }
string PlayerId { get; set; }
}


I plan to write a test for this method and send the sample data for IPlayerRequest to get the response against a deployed end point. I have the TestSetup ready for hitting the endpoint.


What is the ideal way to mock the IPlayerRequest and populate it with some data to get a response?


Aucun commentaire:

Enregistrer un commentaire