C++ 中的模拟对象

Alo*_*ave 6 c++ unit-testing mocking

什么是 Mock 对象?你能解释一下这个概念吗?如何在 C++ 中使用 Mock 对象?任何源示例都将非常有帮助。

Era*_*'er 5

Fake-It是一个简单的 C++ 模拟框架。FakeIt 使用最新的 C++11 功能来创建富有表现力(但非常简单)的 API。使用 FakeIt,无需重新声明方法,也无需为每个模拟创建派生类。以下是你如何假装的:

struct SomeInterface {
  virtual int foo(int) = 0;
};

// That's all you have to do to create a mock.
Mock<SomeInterface> mock; 

// Stub method mock.foo(any argument) to return 1.
When(Method(mock,foo)).Return(1);

// Fetch the SomeInterface instance from the mock.
SomeInterface &i = mock.get();

// Will print "1"
cout << i.foo(10);
Run Code Online (Sandbox Code Playgroud)

还有更多功能值得探索。来吧,尝试一下


Ano*_*non 3

阅读mockcpp,您会找到问题的答案。模拟非常适合测试目的,您可以专注于测试一件事并模拟环境中其他部分的行为。