问题
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