Cómo simular un delegado en xUnit
Tengo delegado esa llamada al método SendMessagesAsync
, que toma un IEnumerable ProviderExemptionReceivedNotification. ¿Cómo simulo SendMessagesAsync que es el callBack del delegado? En otras palabras, ¿cómo hago un mock del Delegate?
Interfaz
public delegate Task
public interface IRingGoApiService
{
Task
}
Implementación del delegado
public async Task
{
var msgSent = await sendProviderExemption(new List
}
Clase de prueba
public MyTestClass{
public MyTestClass()
{
}
private readonly Mock
}
Usando delegado
var result = await _ringGoApiService.ProcessCreateRequestAsync(ringGoExemption,
async (IEnumerable
);
SendMessagesAsync
static async Task
{
SetProviderExemption(exemptions);
var messages = exemptions.Select(exemption =>
{
//Crear mensaje de ServiceBus...
return message;
});
}
static void SetProviderExemption(IEnumerable
{
providerExemption = exemptions.FirstOrDefault();
}
davy.ai
Para simular el delegado
SendAnprProviderExemptionNotifications
, puedes crear unFunc
que tome un parámetroIEnumerable<providerexemptionreceivednotification>
y devuelva unaTask<bool>
. En tu prueba, puedes usar esteFunc
en lugar de llamar al método real.Aquí tienes un ejemplo de cómo podrías actualizar tu clase de prueba para simular el delegado:
En este ejemplo, la variable
sendProviderExemptionMoq
contiene unFunc
configurado para simular el métodoSendMessagesAsync
y devolver un resultado deseado. Luego, se llama al métodoProcessCreateRequestAsync
con la simulación del delegado como parámetro. Por último, se pueden realizar las aserciones necesarias basadas en los resultados.