vendredi 23 janvier 2015

How to unit test Service Stacks Redis Client with Moq

I'm trying to understand how can I mock the IRedisClientsManager so that I can unit test the Handle Method below using Moq.


Cheers



public class PropertyCommandHandler : ICommandHandlerFor<PropertySaveRequest, PropertyCommandResult>
{
private readonly IRedisClientsManager _manager;

public PropertyCommandHandler(IRedisClientsManager manager)
{
this._manager = manager;
}

public PropertyCommandResult Handle(PropertySaveRequest request)
{
request.Property.OwnerId.ValidateArgumentRange();

using (var client =_manager.GetClient())
{
var propertyClient = client.As<Model.Property>();

var propertyKey = string.Format("property:{0}", request.Property.OwnerId);

propertyClient.SetEntry(propertyKey, request.Property);

client.AddItemToSet("property", request.Property.OwnerId.ToString());
}

return new PropertyCommandResult() {Success = true};
}
}


Which I call from the service like so



public class PropertyService : Service, IPropertyService
{
private readonly ICommandHandlerFor<PropertySaveRequest, PropertyCommandResult> _commandHandler;

public PropertyService(ICommandHandlerFor<PropertySaveRequest, PropertyCommandResult> commandHandler)
{
this._commandHandler = commandHandler;
}

public object Post(PropertySaveRequest request)
{
if (request.Property == null)
throw new HttpError(HttpStatusCode.BadRequest, "Property cannot be null");

var command = _commandHandler.Handle(request);
return command;
}
}

Aucun commentaire:

Enregistrer un commentaire