首页 > 解决方案 > 使用 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
   // ..
}

如代码所示,我的函数使用了许多标准库函数。那么我应该模拟标准库函数还是对函数进行实际调用?

标签: c++googletestgooglemock

解决方案


你可以做些什么来测试你的功能:

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
   // ...
}

最后,您可以轻松地模拟IFileManagerIAllocator.

如果没有该接口,您将不得不使标准函数按预期运行,这不是简单可行的(错误的路径,限制内存(哪个限制))

还要注意实施可能有更多限制。来自接口的那个(MAX_PATH、非常规文件、UNC 路径)


推荐阅读