使用 gmock 模拟标准库函数
Mocking standard library functions using 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 路径)
以下是我要进行单元测试的函数:
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 路径)