c++ - 使用 gmock 模拟标准库函数
问题描述
以下是我要单元测试的功能:
void sampleFunc()
{
FILE *file = fopen(path, "rb");
if (!file) {
std::cout << "File couldn't be opened!" << std::endl;
}
const int bufSize = 32768;
unsigned char *buffer = (unsigned char*) malloc(bufSize);
if (!buffer) {
fclose(file);
std::cout << "Failed to allocate buffer for SHA256 computation." << std::endl;
}
// ..
// Read file into buffer
// ..
}
如代码所示,我的函数使用了许多标准库函数。那么我应该模拟标准库函数还是对函数进行实际调用?
解决方案
你可以做些什么来测试你的功能:
class IFileManager
{
public:
virtual ~IFileManager() = default;
// You could even improve interface by returning RAII object
virtual FILE* fopen(const char* path, const char* mode) = 0;
virtual void fclose(FILE*) = 0;
// ...
};
class FileManager : public IFileManager
{
public:
FILE* fopen(const char* path, const char* mode) override { return ::fopen(path, mode); }
int fclose(FILE* f) override { return ::fclose(f); }
// ...
};
class IAllocator
{
public:
virtual ~IAllocator() = default;
virtual void* allocate(std::size_t) = 0;
virtual void deallocate(void*) = 0;
};
class Allocator : public IAllocator
{
public:
void* allocate(std::size_t size) override { return malloc(size); }
void deallocate(void* p) override { free(p); }
};
你的功能变成:
void sampleFunc(IFileManager& fileManager, IAllocator& allocator)
{
FILE *file = fileManager.fopen(path, "rb");
if(!file) {
std::cout << "File couldn't be opened!" << endl;
return;
}
const int bufSize = 32768;
unsigned char *buffer = (unsigned char*) allocator.allocate(bufSize);
if(!buffer) {
fileManager.fclose(file);
std::cout << "Failed to allocate buffer for SHA256 computation." << std::endl;
return;
}
// Read file into buffer
// ...
}
最后,您可以轻松地模拟IFileManager
和IAllocator
.
如果没有该接口,您将不得不使标准函数按预期运行,这不是简单可行的(错误的路径,限制内存(哪个限制))
还要注意实施可能有更多限制。来自接口的那个(MAX_PATH、非常规文件、UNC 路径)
推荐阅读
- javascript - 关于 webpack 中的 nodejs 路径,webpack 如何在 typescript 中查找模块?
- .htaccess - HTACCESS 不在别名站点上重定向
- reactjs - 如何在按钮单击时更新排版文本
- c - 在 C 中的 2d 矩阵中的第二个对角线下打印元素
- python - 使用 pybind11 构建库,链接其他共享库
- python - 使用 selenium 单击按钮并查看更多文本
- excel - 使用 VBA 选择一系列工作表
- string - 如何处理异常 C#?
- json - 将指定的 JSON 对象从数组移动到不同的数组
- android - 以编程方式修改 ConstraintSet 链未按预期工作