依赖注入的NServiceBus处理器单元测试 [英] Dependency injection for NServiceBus handler unit testing

查看:193
本文介绍了依赖注入的NServiceBus处理器单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是你应该如何注入依赖你的NServiceBus处理程序,以测试它:

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

(<一个HREF =http://nservicebus.com/UnitTesting.aspx相对=nofollow> http://nservicebus.com/UnitTesting.aspx )



然而,这意味着我的依赖对象的引用应该是市民,我不喜欢太多。什么办法可以使其保持私有只读,并为其分配的内部构造,因此推测实施只通过处理器的构造函数传递?



感谢

$ B $

 <$ C:将b 
解决方案

您可以通过下面的语法构造函数注入$ C> Test.Handler< YourMessageHandler>(总线= gt;新建YourMessageHandler(DEP1,dep2))

如果DEP1和dep2是在所有的可能性只是一些存根或嘲笑,你的模拟框架炮制你



- 由乌迪大汉从这里更新时间:



您可以通过Test.Bus访问嘲笑总线实例。


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

解决方案

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.

这篇关于依赖注入的NServiceBus处理器单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆