samedi 31 janvier 2015

How to use Redis in unit testing?

Suppose I have a method in my controller class that updates the score of a key,value pair in a Redis database. I want to write a unit test to check if the score is not null and is incremented by 1. I just want to see how unit testing works with Redis and how you extract the score from a particular key, value pair and check its validity.


Controller Class //When user refreshes api/do/v1/togglelike/{id}", the score is updated in redis for that user and is incremented by 1;



[HttpGet]
[Route("api/do/v1/togglelike/{id}")]
public IHttpActionResult ToggleLike(String id)
{
var currentUser = "mike";


var likeSet = redis1.SortedSetRangeByScore("likes:" + id);
var likeStatus = redis1.SortedSetScore("likes:" + id, currentUser);

//Current user has not yet liked the profile
if (likeStatus == null)
{
redis1.SortedSetAdd("likes:" + id, currentUser, 1);
return Ok("Like Added");

}
/*redis1.SortedSetAdd("likes:" + id, currentUser, 1);
return Ok("Like Added");*/
else
{
double counter = redis1.SortedSetIncrement("likes:" + id, currentUser, 1);
redis1.SortedSetAdd("likes:" + id, currentUser, counter);
return Ok("Like Added");
/*redis1.SortedSetRemove("likes:" + id, currentUser);
return Ok("Like Removed");*/
}
}


Test Class: I want to get the score from the key,value pair and check that it is equal to a valid number;



namespace VideoControllerTest
{
[TestClass]
public class VideoControllerTest
{
IDatabase redis1;

public VideoControllerTest()
{
redis1 = RedisFactory.Connection.GetDatabase();
}

[TestMethod]
public void VideoController_Adview()
{
//Arrange
VideoController controller = new VideoController();
//Act
IHttpActionResult actionResult = controller.ToggleLike("video123");

//Assert; Check to see the counter is incremented by 1 and is not null;

}
}
}

Aucun commentaire:

Enregistrer un commentaire