Dependency injection for NServiceBus handler unit testing

时光总嘲笑我的痴心妄想 提交于 2019-12-09 12:09:42

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/10956347/dependency-injection-for-nservicebus-handler-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!