1
votes

I am attempting to write unit tests for my NServiceBus(version 5) message handlers. I am using RabbitMQ and the Outbox feature.

The Outbox feature gives you "DTC" like functionality, HOWEVER, this is achieved by injecting a NHibernateStorageContext into your handler. Any DB changes you make, MUST be done using the database connection from that object. (i.e. all DB writes must occur via the same database connection/transaction).

The problem I am running into is that NHibernateStorageContext does not have a public default constructor. Plus, it does not implement any interfaces! There is no way for me to mock it and pass it in using standard frameworks like MOQ.

At this point, the only solution I can come up with is to use MS Fakes, generate a Fake of the class and pass it down.

Am I missing something? Is there an easier way to do this?

1
While there are other ways to do this calling them easier would be rather opinionated. Tying /coupling your code to classes you don't own makes them rather difficult to test, as your question clearly demonstrates. Try abstracting these dependencies behind interfaces you own and see if that improves testability. Disclaimer: I am not familiar with the tech involved in your question. this is a general statement. Take with a grain of salt if this does not apply to your situation. - Nkosi

1 Answers

0
votes

I'm just wondering if unit testing a handler like that gives you enough coverage? There's a lot more to message handlers than can be covered with a simple unit tests:

  • Have you got the right information in the header?
  • How do you test interactions (more than one message in a handler)?
  • How about more complex saga logic that involves timeouts?

For that a lot more reasons, there's NServiceBus.Testing package and it is strongly recommended to use that for testing your messaging system. There are samples on the documentation page here.