模拟自由功能 [英] Mocking free function

查看:279
本文介绍了模拟自由功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,似乎找不到解决方案。

I am stuck in a problem and can't seem to find the solution.

我使用VS2005 SP1编译代码。

I am using VS2005 SP1 for compiling the code.

我有一个全局函数:

A* foo();

我有一个模拟类

class MockA : public A {
public:
    MOCK_METHOD0 (bar, bool());
    ...
};

在源代码中,访问方式如下: foo ; bar()。我找不到一种方式来模拟这种行为。我不能改变源代码,所以google mock cook book中的解决方案是没有问题的。

In the sources, it is accessed like this: foo()->bar(). I cannot find a way to mock this behavior. And I cannot change the sources, so the solution in google mock cook book is out of question.

任何帮助或指针在正确的方向将受到高度赞赏。 :)

Any help or pointers in the right direction will be highly appreciated. :)

推荐答案

不可能,不改变源代码,或带自己的版本 foo ()

No it's not possible, without changing the sources, or bringing your own version of foo() that is linked with the executable code.

GoogleMock的常见问题它说


我的代码调用静态/全局函数。我可以模拟吗?



您可以,但需要进行一些更改。

My code calls a static/global function. Can I mock it?

You can, but you need to make some changes.

你发现自己需要模拟一个静态函数,这是一个标志,你的模块太紧耦合(和更少的灵活,更少可重用,更少的可测试,等等)。你可能更好地定义一个小的接口,并通过该接口调用该函数,然后可以很容易地嘲笑。

In general, if you find yourself needing to mock a static function, it's a sign that your modules are too tightly coupled (and less flexible, less reusable, less testable, etc). You are probably better off defining a small interface and call the function through that interface, which then can be easily mocked. It's a bit of work initially, but usually pays for itself quickly.

这个Google测试博客 post 说它非常出色。请检查它。

This Google Testing Blog post says it excellently. Check it out.

也可以从 Cookbook


模拟免费功能



可以使用Google Mock来模拟自由函数(即C风格函数或静态方法)。您只需要重写代码以使用接口(抽象类)。

Mocking Free Functions

It's possible to use Google Mock to mock a free function (i.e. a C-style function or a static method). You just need to rewrite your code to use an interface (abstract class).

不是直接调用自由函数(比如说OpenFile),而是为它引入一个接口有一个具体的子类调用自由函数:

Instead of calling a free function (say, OpenFile) directly, introduce an interface for it and have a concrete subclass that calls the free function:

class FileInterface {
 public:
  ...
  virtual bool Open(const char* path, const char* mode) = 0;
};

class File : public FileInterface {
 public:
  ...
  virtual bool Open(const char* path, const char* mode) {
    return OpenFile(path, mode);
  }
};

您的代码应该与FileInterface交谈以打开文件。现在很容易模拟出函数。

Your code should talk to FileInterface to open a file. Now it's easy to mock out the function.

这可能看起来很麻烦,但实际上你经常有多个相关的函数可以放在同一个接口,所以

This may seem much hassle, but in practice you often have multiple related functions that you can put in the same interface, so the per-function syntactic overhead will be much lower.

如果你担心虚拟函数带来的性能开销,并且性能分析确认了你的担心,你可以将它与

If you are concerned about the performance overhead incurred by virtual functions, and profiling confirms your concern, you can combine this with the recipe for mocking non-virtual methods.






正如您在评论中提到的,实际上提供你自己的版本 foo(),你可以很容易地解决这个具有另一个mock类的全局实例:


As you mentioned in your comment that you actually provide your own version of foo(), you can easily solve this having a global instance of another mock class:

struct IFoo {
    virtual A* foo() = 0;
    virtual ~IFoo() {}
};

struct FooMock : public IFoo {
     FooMock() {}
     virtual ~FooMock() {}
     MOCK_METHOD0(foo, A*());
};

FooMock fooMock;

// Your foo() implementation
A* foo() {
    return fooMock.foo();
}

TEST(...) {
    EXPECT_CALL(fooMock,foo())
        .Times(1)
        .WillOnceReturn(new MockA());
    // ...
}

不要忘记清除调用期望,每个测试用例运行后。

Don't forget to clear all call expectations, after each test case run.

这篇关于模拟自由功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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