Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection for NServiceBus handler unit testing

This is how you supposed to inject dependencies for your NServiceBus handler to test it:

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

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

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

Thanks

like image 664
YMC Avatar asked Jun 08 '12 21:06

YMC


1 Answers

You can use constructor injection by using the following syntax:

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

Where dep1 and dep2 are in all likelihood just some stubs or mocks that your mocking framework cooked up for you.

-- Updated by Udi Dahan from here:

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

like image 160
tmesser Avatar answered Oct 15 '22 12:10

tmesser