Estoy desarrollando pruebas para una aplicación. Hay un método que tiene una matriz params
como parámetro. Configuré el método usando Moq, pero cuando ejecuto la prueba, el valor de retorno del método es nulo, lo que significa que no se está burlando.Método de instalación con Params Array
Aquí está un ejemplo de código:
public interface ITicketManager {
string GetFirstTicketInQueueIfMatches(params string[] ticketsToMatch);
}
public class TicketManager : ITicketManager {
private Queue<string> ticketQueue = new Queue<string>();
public string GetFirstTicketInQueueIfMatches(params string[] ticketsToMatch) {
var firstQueuedTicket = ticketQueue.Peek();
var firstQueuedTicketMatchesAnyOfRequested = ticketsToMatch.Any(t => t == firstQueuedTicket);
if(firstQueuedTicketMatchesAnyOfRequested)
return firstQueuedTicket;
return null;
}
}
El ejercicio práctico se ve así:
var mock = new Mock<ITicketManager>();
mock.Setup(m => m.GetFirstTicketInQueueIfMatches(It.IsAny<string>()))
.Returns(p => {
if(p.Contains("A"))
return "A";
return null;
});
Por qué nunca golpea el método burlado?
@DiegoAC: Bueno lo tienes * * Volviendo condicionalmente 'null' de todos modos. ¿Has intentado hacer que vuelva "Foo" en su lugar si 'p' no contiene' "A" '? De esa forma, al menos verías que se invoca ... –
Excelente respuesta Jon. Gracias. En realidad, recibía solo una cadena. La explicación sobre el compilador fue muy útil. –