C++ 无趣的模拟函数调用 bla() && 预期:至少调用一次 bla()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17060980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:53:25  来源:igfitidea点击:

Uninteresting mock function call bla() && Expected: to be called at least once bla()?

c++mockinggoogletestgooglemock

提问by emmerich

I've written a small test with a mocked class. When I run it, first I get the warning that an uninteresting mock function was called and then the test fails because the expectation is not met, which is that the mocked function is called at least once. The funny thing is that that function is called as I see that warning message above.

我用模拟类编写了一个小测试。当我运行它时,首先我收到警告说调用了一个无趣的模拟函数,然后测试失败,因为没有满足期望,即模拟函数至少被调用一次。有趣的是,当我看到上面的警告消息时,该函数被调用。

Do you have any ideas on this matter?

你对这个问题有什么想法吗?

Thank you!

谢谢!

Edit: This is my code structure:

编辑:这是我的代码结构:

class Bla {

public:
    Bla();
    virtual ~Bla();

    virtual float myFunction();
}

class MockBla : public Bla {
    MockBla();
    ~MockBla();
    MOCKMETHOD0(myFunction, float());
}

class CallerClass {

public:
    CallerClass() { MockBla* myMock = new MockBla(); }
    virtual ~CallerClass();

    myCallingFunction() { myMock->myFunction(); }
}

class MyTestClass : public ::testing::Test {
//....
    TEST(myTest, testMyFunction) {
    MockBla mockBla;
    EXPECT_CALL(mockBla, myFunction()).Times(AtLeast(1));

    CallerClass* callerClass;
    callerClass = new CallerClass();

    callerClass->myCallingFunction();

    }
//....
}

Result:

结果:

[ RUN      ] MyTestClass.testMyFunction

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: myFunction()
          Returns: 0
Stack trace:
MyTestClass.cpp:99: Failure
Actual function call count doesn't match EXPECT_CALL(mockBla, myFunction())...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active

回答by Fraser

You need to set the expectations on the actual instance of your mocked class which will be called during the test's execution.

您需要在测试执行期间调用的模拟类的实际实例上设置期望值。

In your case, you're setting the expectations on the object mockBlawhich is only constructed then destructed at the end of the test - it is never used.

在您的情况下,您正在对mockBla仅在测试结束时构造然后销毁的对象设置期望- 它从未使用过。

You'll either need to pass the mock object into the CallerClassto use, or allow the CallerClassto create the mock object as a member variable, but then allow the test access to that actual member (via e.g. a getter or allowing the test to be a friend of the CallerClass).

您要么需要将模拟对象传递CallerClass给要使用的对象,要么允许将CallerClass模拟对象创建为成员变量,然后允许测试访问该实际成员(例如通过 getter 或允许测试成为一个的朋友CallerClass)。

An example of passing the mocked object into the calling class would be something like:

将模拟对象传递给调用类的示例如下:

#include <memory>
#include "gmock/gmock.h"

class Bla {
 public:
  virtual ~Bla() {}
  virtual float myFunction() = 0;
};

class MockBla : public Bla {
 public:
  MOCK_METHOD0(myFunction, float());
};

class CallerClass {
 public:
  explicit CallerClass(Bla* bla) : mBla(bla) {}
  void myCallingFunction() { mBla->myFunction(); }
 private:
  Bla* mBla;
};

TEST(myTest, testMyFunction) {
  std::shared_ptr<Bla> mockBla(new MockBla);
  EXPECT_CALL(*std::static_pointer_cast<MockBla>(mockBla),
              myFunction()).Times(testing::AtLeast(1));

  CallerClass callerClass(mockBla.get());
  callerClass.myCallingFunction();
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}