mercredi 18 février 2015

How to verify action passed into function was called?

I'm struggling with a unit test using Moq to isolate the BL from the DataAccess. Consider the following business logic:



public class MyBusinessLogic
{

private IDataAccess _dataAccess;

public MyBusinessLogic(IDataAccess dataAccess)
{
_dataAccess = dataAccess;
}

public string SendEmail(string username)
{
var emailAddress = _dataAccess.RunCommand<CommandDetails>("GetEmailAddressForUser", AddParametersToGetEmailAddress, new CommandDetails{Username = username});

// do some send email stuff...

return string.Concat("Email sent to ", emailAddress);
}

private void AddParametersToGetEmailAddress(IDbCommand command, CommandDetails details)
{
var p = command.CreateParameter();
p.ParameterName = "username";
p.Value = details.Username;
command.Parameters.Add(p);
}
}


A set of parameter details



public class CommandDetails
{
public string Username {get; set;}
}


And a DataAccess interface



public interface IDataAccess
{
string RunCommand<TCommandDetails>(string command, Action<IDbCommand, TCommandDetails> addParameters, TCommandDetails details);
}


I have a test



var username = @"joebloggs";

var mock = new Mock<IDataAccess>();
mock.Setup(x => x.RunCommand<CommandDetails>(
It.Is<string>(commandString => commandString == "GetEmailAddressForUser"),
It.IsAny<Action<IDbCommand,CommandDetails>>(),
It.Is<CommandDetails>(details => details.Username == username)))
.Returns("joebloggs@hotmail.com");

var businessLogic = new MyBusinessLogic(mock.Object);

var message = businessLogic.SendEmail(username);

Assert.AreEqual("Email sent to joebloggs@hotmail.com", message);


This is great, it proves that the SP was called and the email was sent to the address returned.


However, it does not prove that the AddParametersToGetEmailAddress method was called. For all we know the SP is being called with no parameters.


How can I modify my setup to only return the email address the username parameter has been set on the command?


Aucun commentaire:

Enregistrer un commentaire