写C++用“伪造”测试一些具有替代实现的方法
我正在使用googletest在C ++中编写测试,并希望改进它们。
我试图描述自己拥有的和要实现的目标:
我有一个具有B类作为成员的实例的A类A:
class ClassA
{
public:
//Some functions
protected:
ClassB m_b;
};
class ClassB
{
void Init(const string &sArgument);
};
在我的测试中,我创建了一个classa实例,并调用了它的函数。在此函数中,将调用M_B的功能INIT()。
因为classB的init()做出了我不想在测试中使用的东西,所以我想调用替代实现。我的第一个目的是与classB和我仅用于测试创建的新类实现的函数init()界面。不幸的是,要这样做,我必须创建一个接口的指针并将其提供给Classa。
__interface IB
{
void Init(const string &sArgument);
};
class ClassB : public IB
{
void Init(const string &sArgument) override;
};
class ClassA
{
public:
ClassA(IB* b) : m_b(b){}
//Some functions
protected:
*IB m_b;
};
对于我的测试,我创建了一个新类:
class ClassBForTest : public IB
{
void Init(const string &sArgument) override;
};
这不是一个好的解决方案,因此我试图找到另一种方法。我实际上发现的是Typemock的库隔离器++,它运行良好。我可以说,对于classB的所有未来实例,方法init都应替换为另一个实例。最好的是,我不必在课堂上进行任何更改。我只需要创建一个新的:
class ClassBForTest
{
void ISOLATOR_TESTABLE Init(const string &sArgument){}
};
在我的测试中,我使用此信息:
ClassBForTest c;
ClassB* b = FAKE_ALL<ClassB>();
WHEN_CALLED(b->Init(ANY_VAL(string))).DoMemberFunctionInstead(&c, Init);
不幸的是,当我想在构建服务器上使用它时,这个库是免费的。因此,我正在寻找替代方案。当我们在其他测试中使用Gmock时,我试图用Gmock意识到这一点,但尚未起作用。我知道这不是一个很好的陈述,但我不希望您的代码。我只想知道Gmock是否可以使用(说应该在此时使用classB实例的替代实现)。
或者,我要求您提供可以使用的其他免费库。
非常感谢 迈克尔
I'm writing tests in C++ using googleTest and want to improve them.
I try to describe what I have and what I want to achieve:
I have a class A that has an instance of class B as a member like this:
class ClassA
{
public:
//Some functions
protected:
ClassB m_b;
};
class ClassB
{
void Init(const string &sArgument);
};
In my test I create an instance of ClassA and call a function of it. In this function the function Init() of m_b will be called.
Because Init() of ClassB makes something that I don't want to have in my test I want to call an alternate implementation. My first intention was to create an interface with the function Init() that is implemented by ClassB and by a new class that I create only for the test. Unfortunately to do this I have to create a pointer of the interface and to give it to ClassA.
__interface IB
{
void Init(const string &sArgument);
};
class ClassB : public IB
{
void Init(const string &sArgument) override;
};
class ClassA
{
public:
ClassA(IB* b) : m_b(b){}
//Some functions
protected:
*IB m_b;
};
For my test I create a new class:
class ClassBForTest : public IB
{
void Init(const string &sArgument) override;
};
This is not a good solution so I tried to find another way. What I actually found is the library Isolator++ by Typemock which works quite good. I can say that for all future instances of ClassB the method Init should be replaced with another one. And the best is that I don't have to make any changes in my ClassA. I just have to create a new one:
class ClassBForTest
{
void ISOLATOR_TESTABLE Init(const string &sArgument){}
};
In my test I use this:
ClassBForTest c;
ClassB* b = FAKE_ALL<ClassB>();
WHEN_CALLED(b->Init(ANY_VAL(string))).DoMemberFunctionInstead(&c, Init);
Unfortunately this library is not for free when I want to use it on a build server. So I'm searching for an alternative. As we used gMock in our other tests I tried to realize this with gmock but it didn't work yet. I know that this is not a good statement but I don't want code from you. I only want to know if this is possible with gMock (saying that an alternate implementation should be used without having the instance of ClassB at this point).
Alternatively I ask you for other free libraries I can use.
Thanks a lot in advance,
Michaela
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论