Re:测试私有方法 [英] Re: Testing private methods

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

问题描述

嗨。


Thomas Mlynarczyk写道:


最后一个选项似乎是迄今为止最好的,但我''我仍然不太高兴

我不得不做私人和误报私人方法为

受保护。



为什么你希望它们首先是私人的?我还没有看到

代码,这真的很有意义......

如果确实如此,那么外部公共API是重要的,如果代码

作为一个整体仍然提供它所承诺的,并且可以测试,它

应该没问题。


问候,

Karsten

解决方案

Karsten Dambekalns写道:


为什么你希望它们首先是私人的?我还没有看到

代码,这真的很有意义......

如果确实如此,那么外部公共API是重要的,如果代码

作为一个整体仍然提供它所承诺的,并且可以测试,它

应该没问题。


问候,

Karsten



走得更远,如果方法是私有的可能是因为它们是由范围访问的
可能又是公共方法的方法。

另一种方法IMO是通过调用(使用try..catch或返回vars)从私有

方法中捕获此错误),然后

仅对呼叫者和公共方法应用测试方法。

Thiago


Karsten Dambekalns schrieb:


如果确实如此,那么外部公共API是重要的,如果代码

作为一个整体仍然提供什么它承诺,这可以证明特德,它

应该没问题。



如果这样的测试失败,就很难找到问题的原因。这就是为什么绝对有必要测试(通常是

较低级别)私人方法,以确保他们做正确的事。


问候,

托马斯


-

Ce n''est pas parce qu''ils sontnombreuxàavoirtort qu'' ils ont raison!

(Coluche)


Thomas Mlynarczyk写道:


Karsten Dambekalns schrieb:


>如果确实如此,那么外部公共API是重要的,如果
代码作为一个整体仍然提供它的东西承诺,并且可以进行测试,应该没问题。



如果这样的测试失败,就很难找到问题的原因。这就是为什么绝对有必要测试(通常是

较低级别)私人方法,以确保他们做正确的事。


问候,

Thomas



然后编写类的公共方法来测试私有方法。

当你''通过测试,摆脱测试方法。


但是,我从不测试这样的私有方法。更重要的是

是他们在班级*内正常工作*。例如 -

私有方法可以正常工作 - 直到其中一个公共

方法传递不正确(或意外)的数据......


-

==================

删除x来自我的电子邮件地址

Jerry Stuckle

JDS计算机培训公司
js ******* @ attglobal.net

==================


Hi.

Thomas Mlynarczyk wrote:

The last option seems the best so far, but I''m still not quite happy as
I''d have to do without "private" and mis-declare private methods as
protected.

Why do you want them to be private in the first place? I have yet to see
code where this really makes sense...

And if it does, then the external public API is what counts, if the code
as a whole still delivers what it promises, and that can be tested, it
should be fine.

Regards,
Karsten

解决方案

Karsten Dambekalns wrote:

Why do you want them to be private in the first place? I have yet to see
code where this really makes sense...

And if it does, then the external public API is what counts, if the code
as a whole still delivers what it promises, and that can be tested, it
should be fine.

Regards,
Karsten

Going farther, if the methods are private probably is because they are
accessed by scope methods which, probably again, are public methods.
An alternative approach IMO is to catch this errors from private
methods on the calls (with try..catch or returning vars), and then
apply an testing method only for the callers, the public methods.
Thiago


Karsten Dambekalns schrieb:

And if it does, then the external public API is what counts, if the code
as a whole still delivers what it promises, and that can be tested, it
should be fine.

If such a test fails, it would be difficult to track down the cause of
the problem. That''s why it is absolutely necessary to test the (usually
lower level) private methods to make sure they do their part right.

Greetings,
Thomas

--
Ce n''est pas parce qu''ils sont nombreux à avoir tort qu''ils ont raison!
(Coluche)


Thomas Mlynarczyk wrote:

Karsten Dambekalns schrieb:

>And if it does, then the external public API is what counts, if the
code as a whole still delivers what it promises, and that can be
tested, it should be fine.


If such a test fails, it would be difficult to track down the cause of
the problem. That''s why it is absolutely necessary to test the (usually
lower level) private methods to make sure they do their part right.

Greetings,
Thomas

Then write public methods of the class to test the private methods.
When you''re through testing, get rid of the test methods.

However, I never test private methods like that. What''s more important
is that they work properly *within the class*. For instance - the
private method may work perfectly correctly - until one of the public
methods passes it incorrect (or unexpected) data...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


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

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