I have the following class where all the logic that I want to unit test happens within a private method I extracted for reuse for the two Handle methods to invoke. What I am stuck on is the proper way to unit test this class. There are essentially 4 observable behaviors I want to assert:
- Does not call 'Replace' when a registration does not exist
- Does not call 'Replace' when a document does not exist
- Does not call 'Replace' when the document message is current
- Calls 'Replace' when all other conditions succeed
I could easily duplicate all the unit tests for the two public Handle methods. But I was wondering if it would be more appropriate to extract the private method out to a class that exposes this behavior using a public method I can directly test against.
Thoughts?
public class Denormalizer :
INotificationHandler<LightsMessageReceived>,
INotificationHandler<AnnouncementsMessageReceived>
{
public Task Handle(AnnouncementsMessageReceived notification, CancellationToken cancellationToken)
{
return HandleMessage(notification, document => document.Announcements, cancellationToken);
}
public Task Handle(LightsMessageReceived notification, CancellationToken cancellationToken)
{
return HandleMessage(notification, document => document.Lights, cancellationToken);
}
private async Task HandleMessage(InventoryMessage message, Func<SaleRegistration, ISequenced> getMessageId, CancellationToken cancellationToken)
{
var registration = await docContext.CsoMessage.GetSaleRegistration(message.SiteId, message.InventoryId, cancellationToken);
if (registration == null) return;
var document = await docContext.SaleRegistration.Get(message.SiteId, message.InventoryId,
registration.Data.SaleDate, registration.Data.SaleType, cancellationToken);
if (document == null) return;
if (getMessageId(document).IsCurrent(message.MessageId)) return;
document.Map(message);
await docContext.SaleRegistration.Replace(document);
}
}
Aucun commentaire:
Enregistrer un commentaire