Dependency Identification for Testing NServiceBus

Here's how you should inject dependencies for your NServiceBus handler to test it:

Test.Handler<YourMessageHandler>() .WithExternalDependencies(h => h.Dependency = yourObj) 

( http://nservicebus.com/UnitTesting.aspx )

However, this means that the reference to the Dependency object must be public, which I do not like. Is there any way to keep it private readonly and assign it inside the constructor, so that the implementation should only be passed through the handler constructor?

thanks

+6
source share
1 answer

You can use constructor injection using the following syntax:

  Test.Handler<YourMessageHandler>(bus => new YourMessageHandler(dep1, dep2)) 

Where dep1 and dep2, in all likelihood, are just some of the stubs or jokes that your mocking structure has prepared for you.

- Updated Udi Dahan from here:

You can access the mocked bus instance through Test.Bus.

+5
source

Source: https://habr.com/ru/post/917660/


All Articles