Mock non-virtual method giving compilation error

前端 未结 2 999
清歌不尽
清歌不尽 2021-01-14 21:16

I need to write the gtest to test some existing code that has a non-virtual method, hence I am testing using the below source, but I am getting the compilation error

2条回答
  •  失恋的感觉
    2021-01-14 21:27

    If you don't want to change your source code, you can leverage injector++. Currently it only supports x86 Windows. But Linux and x64 Windows support will come soon. Below examples will give you a brief idea:

    Mock non-virtual methods

    Below example fakes BaseClassTest::getAnInteger() by using fakeFunc():

    class FakeClassNonVirtualMethodTestFixture : public ::testing::Test
    {
    public:
        int fakeFunc()
        {
            return 6;
        }
    };
    
    TEST_F(FakeClassNonVirtualMethodTestFixture, FakeIntFunctionWhenCalled)
    {
        // Prepare
        int expected = 6;
        InjectorPP::Injector injector;
    
        injector.whenCalled(INJECTORPP_MEMBER_FUNCTION(BaseClassTest::getAnInteger))
            .willExecute(INJECTORPP_MEMBER_FUNCTION(FakeClassNonVirtualMethodTestFixture::fakeFunc));
    
        BaseClassTest b = BaseClassTest();
    
        // Act
        // FakeFunc will be executed!
        int actual = b.getAnInteger();
    
        // Assert
        EXPECT_EQ(expected, actual);
    }
    

    Mock virtual methods

    Injector++ supports virtual method mocking (Amazing, huh?). Below is a simple example:

    int FakeIntFuncForDerived()
    {
        return 2;
    }
    
    TEST_F(FakeClassVirtualMethodTestFixture, MockDerivedClassVirtualMemberFunctionWhenCalled)
    {
        // Prepare
        int expected = 2;
        BaseClassTest* derived = new SubClassTest();
    
        InjectorPP::Injector injector;
        injector.whenCalledVirtualMethod(derived, "getAnIntegerVirtual")
            .willExecute(fakeIntFuncForDerived);
    
        // Act
        // FakeIntFuncForDerived() will be exectued!
        int actual = derived->getAnIntegerVirtual();
    
        // Assert
        EXPECT_EQ(expected, actual);
    
        delete derived;
        derived = NULL;
    }
    

    Mock static methods

    Injector++ supports static method mocking. Below is a simple example:

    Address FakeGetAnAddress()
    {
        Address addr;
        addr.setAddressLine("fakeAddressLine");
        addr.setZipCode("fakeZipCode");
    
        return addr;
    }
    
    TEST_F(FakeClassNonVirtualMethodTestFixture, FakeStaticFunctionReturnUserDefinedClassWhenCalled)
    {
        // Prepare
        Address expected;
        expected.setAddressLine("fakeAddressLine");
        expected.setZipCode("fakeZipCode");
    
        InjectorPP::Injector injector;
    
        injector.whenCalled(INJECTORPP_STATIC_MEMBER_FUNCTION(BaseClassTest::getAnAddressStatic))
            .willExecute(INJECTORPP_MEMBER_FUNCTION(FakeClassNonVirtualMethodTestFixture::fakeGetAnAddress));
    
        // Act
        // FakeGetAnAddress will be executed!
        Address actual = BaseClassTest::getAnAddressStatic();
    
        // Assert
        EXPECT_EQ(expected, actual);
    }
    

提交回复
热议问题