Home > Backend Development > C++ > How to mock external dependencies in C++ unit tests?

How to mock external dependencies in C++ unit tests?

WBOY
Release: 2024-04-24 09:12:01
Original
974 people have browsed it

There are three ways to mock external dependencies in C: 1. Stubbing (predefined behavior), 2. Stubbing (logging calls), 3. Injection (passing mocks as arguments). Through stubbing, stubbing, and injection, you can isolate components and create robust unit tests.

C++ 单元测试中如何模拟外部依赖项?

C How to simulate external dependencies in unit testing

In unit testing, simulating external dependencies is important for isolating each component. Testing is very important. The following three methods can easily simulate external dependencies in C:

1. Stub

A stub is a simulation that provides predefined behavior , without calling the actual dependencies. To create stubs, you can use a mocking framework such as Google Mock or CMocka.

// 使用 Google Mock 创建一个桩:
class MockExternalDependency {
 public:
  MOCK_METHOD(int, Function1, (int a, int b));
};

// 创建桩并用它替换实际的依赖项:
MockExternalDependency mockDependency;
EXPECT_CALL(mockDependency, Function1(1, 2)).WillOnce(Return(3));

// 测试代码:
int result = TestFunction(&mockDependency);
ASSERT_EQ(result, 3);
Copy after login

2. Stub

A stub is similar to a stub, but it does not predefine behavior. Instead, it logs calls to dependencies and allows you to inspect them. This is useful for checking input and call order.

// 使用 CMocka 创建一个存根:
void external_dependency_stub(int a, int b) {
  // 记录被调用的参数
}

// 设置存根并测试代码:
cmocka_set_stub(external_dependency, external_dependency_stub);
TestFunction();

// 检查存根中记录的调用:
ASSERT_TRUE(cmocka_call_count(external_dependency) > 0);
Copy after login

3. Injection

Injection involves passing dependencies as parameters to the function under test. This allows you to easily replace a dependency's implementation to use mocks during testing.

// 使用依赖项注入进行测试:
void TestFunction(ExternalDependency& dependency) {
  // 使用模拟依赖项调用函数:
  MockExternalDependency mockDependency;
  EXPECT_CALL(mockDependency, Function1(1, 2)).WillOnce(Return(3));
  int result = TestFunction(&mockDependency);
  ASSERT_EQ(result, 3);
}
Copy after login

Practical case:

Suppose we have a function ReadFile, which depends on the # in the external class FileSystem ##OpenFile and ReadFile methods. In order to unit test ReadFile we need to mock FileSystem:

class MockFileSystem {
 public:
  MOCK_METHOD(FILE*, OpenFile, (const char* filepath));
  MOCK_METHOD(int, ReadFile, (FILE* file, char* buffer, int size));
};

void TestReadFile() {
  MockFileSystem mockFileSystem;
  FILE* mockFile = fopen("mockfile.txt", "w");
  EXPECT_CALL(mockFileSystem, OpenFile("mockfile.txt")).WillOnce(Return(mockFile));
  EXPECT_CALL(mockFileSystem, ReadFile(mockFile, testing::_, _))
      .WillOnce(SetArrayArgument<1>("Hello", 5));

  char buffer[10];
  int result = ReadFile("mockfile.txt", buffer, 10, &mockFileSystem);
  ASSERT_EQ(result, 5);
  ASSERT_STREQ(buffer, "Hello");
}
Copy after login

The above is the detailed content of How to mock external dependencies in C++ unit tests?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template