测试使用RhinoMocks的私有方法 [英] Testing private methods using RhinoMocks

查看:280
本文介绍了测试使用RhinoMocks的私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我TDD环境下工作,基本上我一个两难我想这是在TDD环境非常重要面向。作为一个程序员,你希望你的方法,以尽可能可读。为了实现这一目标,我们倾向于分割我们在多个私有方法的方法为好。虽然这样做,它被转移到私有函数所有的代码失去它的测试能力。

I work in TDD environment and basically I am facing with a dilemma which I think is very important in TDD environment. As a programmer, you want your methods to be as readable as possible. To achieve that, we tend to partition our methods in multiple private methods as well. While doing that all that code which was moved to the private function looses it's test ability.

犀牛测试类不能看到所有的私有方法,我需要能够运行针对这些方法测试以及。我不希望他们是公众,因为它没有任何意义,让他们公开。

Rhino test class cannot see all those private methods and I need to be able to run tests against those methods as well. I do not want them to be public because it does not make sense to keep them public.

任何想法?

推荐答案

如果我qoute你的问题的一部分:

If I qoute a part of your question:

[...]我们倾向分区我们在多个私有方法的方法[...]

[...] we tend to partition our methods in multiple private methods [...]

这是错误的。如果你遵循单一职责原则和良好的面向对象设计,你的方法会更独立,更简单。如果你觉得你要解开一个又一个的私人的方法,使您的公共的一看就短,第一次给它一个想法。也许,你可以重构它在一个单独的类?

This is wrong. If you follow a single responsibility principle and good OOP design, your methods would be much independent and simpler. If you feel like you want to extract a yet another private method to make your public one look shorter, give it a thought first. Maybe, you can refactor it in a separate class?

您不测试的私人的方法,因为你测试的公共合同,实现的不是细节。如果你想有远亲类似的私人的方法测试,让他们的内部的并设置的 InternalsVisibleTo 属性。

You do not test private methods, because you test public contracts and not the details of implementations. If you want to have something distantly similar to private methods testing, make them internal and set InternalsVisibleTo attribute.

另一个法(R.哈维指出)是写一个包装你的私人的方法引入的公共的那些包装类。这种方法的,你不需要做出有利于你的私人的方法内部的。其缺点是,对于每一个私人的方法,你将有一个包装的公共的方法。这样的方法的量可以增加一倍。

Another method (pointed by R. Harvey) is to write a wrapper class that wraps you private methods into public ones. This approach has a benefit that you don't need to make your private methods internal. The downside is that for every private method you will have a wrapper public method. So the amount of methods may double.

这篇关于测试使用RhinoMocks的私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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