mercredi 6 mai 2015

Using NserviceBus.Testing how do you test the published event when the code is not initiated via a message handler

Question

If the current method being tested is initiated via some other method than an incoming message (into a handler) how do you test the publishing of events. The problem with NServiceBus.Testing (as far as I see it) is that its very geared towards testing handlers which in turn cause messages/events to be send/published.

Background:

We have a number of legacy systems, and in the ongoing effort to move to a proper SOA implementation we need to integrate with some legacy DB jobs. These jobs perform actions on the DB every 15 minutes which we want to hook into and publish events when certain conditions occur.

We have a windows service that is running within the NsbHost but that contains no handlers. Instead we are using Quartz to create an internal cron job that runs every minute, polls the DB for records that match a specified pattern, and publishes an event before updating the DB to say we have processed that record. This is how we are integrating old and new systems.

Clearly this is not an ideal situation but given that we are in a transition phase in our SOA implementation its as good as we have right now.

Details and code:

Our event interface we are publishing looks like this

    public interface IPremiumAdjustmentFinalised
    {
        string PolicyNumber { get; set; }
        decimal Amount { get; set; }
        DateTime FinalisedOn { get; set; }
    }

The code to actually publish the event is

    Bus.Publish<IPremiumAdjustmentFinalised>(e =>
    {
        e.Amount = adj.AdjustmentAmount;
        e.PolicyNumber = adj.AdjustmentProfile.PolicyNumber;
        e.FinalisedOn = adj.LastModifiedOn;
    });

This all works fine and we can test the call was made using MOQ thus:

    eventPublisher.MethodToTest();
    bus.Verify(x => x.Publish(It.IsAny<Action<IPremiumAdjustmentFinalised>>()), Times.Once);

where bus is a new Mock inserted into the constructor.

But I want to test the values within IPremiumAdjustmentFinalised. I'm finding this really difficult I think mainly because its an interface rather than a concrete class.

We have tried using NServiceBus.Testing to try and inspect the generated event but to no avail.

Does anyone know of how to test the values in the event in this given scenario?

Aucun commentaire:

Enregistrer un commentaire