Google Mock:是否可以使用全局模拟对象? [英] Google Mock: Is it ok to use global mock objects?

查看:174
本文介绍了Google Mock:是否可以使用全局模拟对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有关 gmock 的所有文档中,我总是找到要在测试中实例化的模拟对象,像这样:

In all the documentation about gmock I always find the mock object to be instantiated inside a test, like that:

TEST(Bim, Bam)
{
    MyMockClass myMockObj;
    EXPECT_CALL(MyMockObj, foo(_));
    ...
}

因此,每个测试都会创建和销毁对象.我相信按测试治具创建和销毁对象也很好.但我想知道是否也可以使用模拟对象的文件全局实例,例如:

So, the object is created and destroyed per test. I believe it's also perfectly fine to create and destroy the object per test fixture. But I'm wondering if it's also ok to have a file-global instance of the mock object, like that:

MyMockClass myMockObj;

TEST(Bim, Bam)
{
    EXPECT_CALL(MyMockObj, foo(_))
    ...
}

我尝试了一下,到目前为止,我绝对没有问题,一切似乎都很好.但是也许我应该知道什么?只是因为我偶然发现了这个问题,其中唯一的答案是:

I tried it and I have absolutely no problems so far, it all seems to work fine. But maybe I should be aware of anything? Just because I stumbled about this question, where the only answer states:

...问题是您正在实例化FooMock的全局实例. Googlemock/googletest希望在测试的主体内或测试夹具类中定义该模拟.

... the problem is that you're instantiating a global instance of FooMock. Googlemock/googletest expect the mock to be defined either within the body of the test, or within a test fixture class.

但是我在文档或其他任何地方都找不到能证实这一点的东西(我是否忽略了它?).

But I could not find anything in the documentation or anywhere else that confirms this (did I overlook it?).

谢谢Georg

PS:之所以需要使用全局模拟实例是另一个讨论的主题(请参见此信息).

PS: The reason why I need to use a global mock instance would be the topic of another discussion (see this posting of mine).

推荐答案

可以,但这不是一个好主意.

You can, but it is not a good idea.

这样做会违反UT的隔离原则. 这种违反可能会导致您的测试意外失败/通过.

Doing such a thing is violate the isolation principle of UT. This violation may cause an unexpected failure/pass in your tests.

Gtest使用伪造对象的析构函数来验证是否发生了期望,这就是期望每个伪造对象将在测试的主体中或测试夹具类中创建并释放的原因.

Gtest uses the destructor of the fake objects to verify that the expectation occurred, this is the reason behind the expectation that each fake object will create and release in the body of the test, or within a test fixture class.

如果将伪造的对象设置为全局对象,那么它将不会在每个UT的末尾释放,那么验证将不会执行,并且即使失败,测试也将通过.当您同时执行所有测试时,您的某些UT可能会失败/失败;在一个测试中,您期望方法x将不会被调用,而在另一个测试中,您期望该方法将被调用;在一个UT中,您希望方法x将被调用3次,但是该方法在测试中被调用两次,而在其他测试中被调用一次(测试应该失败,但不会...)

If you make the fake object global then it won't release at the end of each UT, then the verification won't execute and the test will pass even when it should fail. more over some of your UTs may fass/fail when you execute all your tests together; in one test you expect the method x won't call and in the other you expect that the method will call; in one UT you expect the method x will call 3 times, but the method was call twice in the test + one in other test(the test should fail but it won't...)

因此,最重要的是,除非该全局模拟仅用于防止空指针(您未设置行为),否则永远不要使用全局模拟.

So the bottom line you should never use a global mock unless this global mock is being in use only to prevent null pointer(you didn't set a behaviour..)

这篇关于Google Mock:是否可以使用全局模拟对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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