测试受保护或私有功能? [英] Testing protected or private functionality?

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

问题描述

我正在使用我在Thinking in C ++中找到的一个简单的单元测试架构。

我刚刚决定将一些成员函数放在公共场所
进入受保护的范围。问题是我的测试想要访问那些

函数......真可惜。当面对这个问题时,还有其他人做了什么?或者只是测试功能的一般问题

以私密方式封装?

I''m using a simple unit test architecture I found in "Thinking in C++".
I just decided I wanted to place some member functions out from public
into a protected scope. Problem is that my tests want to access those
functions...bummer. What have other people done when faced with this
problem or just the general problem of testing functionality
encapsulated in a private manner?

推荐答案

Noah Roberts写道:
Noah Roberts wrote:
我正在使用一个简单的单元测试架构,我在Thinking in
C +中找到+ QUOT ;.我刚刚决定将一些成员函数从公共场所放到受保护的范围内。问题是我的测试想要访问这些功能......真糟糕。当面对这个问题或者只是以私密方式封装测试功能的一般问题时,其他人做了什么?
I''m using a simple unit test architecture I found in "Thinking in
C++". I just decided I wanted to place some member functions out from
public into a protected scope. Problem is that my tests want to
access those functions...bummer. What have other people done when
faced with this problem or just the general problem of testing
functionality encapsulated in a private manner?




复制测试类中的功能和#ifdef它以便

最终产品没有它:


A类{

void someprivatefunction(int);

public:

#ifdef DO_TEST

void test(){

someprivatefunction(42);

断言(somepostcondition);

}

#endif

};


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问



Duplicate the testing functionality in the class and #ifdef it so that
the final product doesn''t have it:

class A {
void someprivatefunction(int);
public:
#ifdef DO_TEST
void test() {
someprivatefunction(42);
assert(somepostcondition);
}
#endif
};

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Noah Roberts写道:
Noah Roberts wrote:
我正在使用我在Thinking in C ++中找到的简单单元测试架构。
我刚决定将一些成员函数放在公共场所进入受保护的范围。问题是我的测试想要访问那些功能......无赖。当面对这个问题时,还有其他人做了什么,或者只是以私密方式封装了测试功能的一般问题?
I''m using a simple unit test architecture I found in "Thinking in C++".
I just decided I wanted to place some member functions out from public
into a protected scope. Problem is that my tests want to access those
functions...bummer. What have other people done when faced with this
problem or just the general problem of testing functionality
encapsulated in a private manner?



您可以进行测试给朋友上课,或者将私人

方法重构成另一个对象并进行测试。


-

Ian Collins。


You could either make the test class a friend, or refactor the private
methods into another object and test that.

--
Ian Collins.




Ian Collins写道:

Ian Collins wrote:
Noah Roberts写道:
Noah Roberts wrote:
我是使用我在Thinking in C ++中找到的简单单元测试架构。
我刚决定将一些成员函数从公共场所放入受保护的范围。问题是我的测试想要访问那些功能......无赖。当面对这个问题时,还有其他人做了什么,或者只是以私密的方式封装了测试功能的一般问题?
I''m using a simple unit test architecture I found in "Thinking in C++".
I just decided I wanted to place some member functions out from public
into a protected scope. Problem is that my tests want to access those
functions...bummer. What have other people done when faced with this
problem or just the general problem of testing functionality
encapsulated in a private manner?


您可以让测试类成为朋友,或者将私有方法重构为另一个对象并对其进行测试。


You could either make the test class a friend, or refactor the private
methods into another object and test that.




这对于私有,这是一种暴露的种类,但是对于

受保护的成员可能会有点问题。



That would work ok for privates, an exposed pimpl of sorts, but for
protected members it could be a bit problematic.


这篇关于测试受保护或私有功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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