0

我正在尝试将 CppuTest 添加到函数 TestFunc(),该函数使用在 helperfunction.hpp 中定义的静态内联函数helperFunc() 。helperfunction.hpp 作为头文件添加到我要测试的文件中。

我想在 helperfunctionMock.hpp 中使用静态内联 helperFunc() 的模拟版本,而不是真正的 helperFunc()。我将 helperfunctionMock.hpp 添加到测试文件中。

当我单独从模拟中调用 helperFunc() 时,它工作正常。但是当我调用 TestFunc() 时,它在 CppUTest 中给了我一个分段错误。我相信这是由于静态内联 helperFunc()的两个定义。构建和执行测试文件时如何避免此错误?

FileUnderTest.cpp

#include "FileUnderTest.hpp"
#include "helperfunction.hpp"

bool functionUnderTest()
{
    if( !helperFunc())
    {
        return false;
    }

    return true;
}

辅助函数.hpp

static inline int32_t helperFunc()
{  
    var = 10;
    return var;
}

TestFileUnderTest.cpp

#include "FileUnderTest.hpp"
#include "helperfunctionMock.hpp"

void runTest()
    {
        // This works okay
        mock("helper_functions")
            .expectOneCall("helperFunc")
            .andReturnValue(1);      
        mock().ignoreOtherCalls();

        auto temp = helperFunc();
        CHECK_TRUE(temp);
    
        // This gives me segmentation fault
        mock("helper_functions")
            .expectOneCall("helperFunc")
            .andReturnValue(1);      
        mock().ignoreOtherCalls();

        auto temp = functionUnderTest();
        CHECK_TRUE(temp);
    }

最后,helperfunctionMock.hpp

#pragma once
#include <CppUTestExt/MockSupport.h>

static inline int32_t helperFunc()
{
    mock("helper_functions").actualCall("helperFunc");

    if (mock("helper_functions").hasReturnValue())
    {
        return mock("helper_functions").intReturnValue();
    }

    return 1;
}
4

0 回答 0