C++ 使用gmock模拟标准库函数

C++ 使用gmock模拟标准库函数,c++,googletest,googlemock,C++,Googletest,Googlemock,以下是我想进行单元测试的函数: void sampleFunc() { FILE *file = fopen(path, "rb"); if (!file) { std::cout << "File couldn't be opened!" << std::endl; } const int bufSize = 32768;

以下是我想进行单元测试的函数:

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
   // ..
}
void sampleFunc()
{
FILE*FILE=fopen(路径“rb”);
如果(!文件){

std::cout您可以做些什么来测试您的功能:

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
   // ...
}
void sampleFunc(IFileManager和fileManager、IAllocator和分配器)
{
FILE*FILE=fileManager.fopen(路径“rb”);
如果(!文件){

std::cout您可以做些什么来测试您的功能:

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
   // ...
}
void sampleFunc(IFileManager和fileManager、IAllocator和分配器)
{
FILE*FILE=fileManager.fopen(路径“rb”);
如果(!文件){

std::可能取决于要测试的内容,但在某些情况下,您需要知道普通mock会忘记的标准调用的“限制”(最大路径限制、特殊字符处理、UNC路径、非常规文件…)例如,我希望有一些测试用例,例如1)文件无法打开,例如fopen抛出错误。2)fopen显示成功,Mallon失败。这取决于您要测试的内容,但在某些时候,您需要知道普通mock会忘记的标准调用的“限制”(最大路径限制、特殊字符处理、UNC路径、非常规文件…)例如,我想有一些测试用例,比如1)文件无法打开,例如fopen抛出错误。2)fopen显示成功,Mallon失败。我想这简单地总结了我想做的事情。谢谢。我想这简单地总结了我想做的事情。谢谢。