开发者

.NET 4 makes for better unit testing/mocking due to the ability to monkey patch?

Consider this about the up-coming Iron Python implementation.

In theory it would allow monkey patching right? Good or bad, it matters not. Given someone else's non-testable code (at least easily) the dynamic keyword would allow monkey patching would it not? This has great potential for C# and VB if I'm reading it right.

What are the thoug开发者_如何学Gohts on this?


The dynamic type support in .Net 4 does not really give you full monkey-patching support across the CLR type system. It give you another way to mock objects, but to be frank the current way is plenty good (see moq: http://code.google.com/p/moq/)

With dynamic types, you lose intellisense, so an argument could be made that it is not advisable to dynamic for mocking.

The IronPython style interception will allow you to wrap up an existing object with your own desired behavior, but it will not allow you to tell the framework, to patch all Foo objects (from this point onwards) so Bar method will call Bar2 instead of Bar.

Keep in mind, with IronRuby and IronPython there are 2 separate type systems in play, there is the underlying CLR type system and the IronRuby/IronPython type system, when they call out to C# code there is marshalling going on. So even though IronRuby/IronPython can properly monkey patch their own type system, they can not use the same mechanism to patch the CLRs type system.

If you want monkey patching you need proper interception and that is hard: Dynamic interception of calls in .NET

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜