meekrosoft / fff

A testing micro framework for creating function test doubles
Other
761 stars 167 forks source link

Fake function problem #55

Closed louiske0911 closed 5 years ago

louiske0911 commented 6 years ago

Hi, could I still use the original function after I faked this function in my unit test?

wulfgarpro commented 5 years ago

@ekeroc, as mentioned, use the language features. Keep a function pointer and store the original function before assigning the custom delegate.

Feel free to ask for more information or, if you've found a solution, post an example.

KasperZK commented 2 years ago

@wulfgarpro could you elaborate please? I am having the same issue. I am trying to fake functionA, which lives in .h file which I have included in my .cpp unit test file. I am trying to fake the function with FAKE_VALUE_FUNC(bool, functionA); but I get the errors "one or more multiply defined symbols found" and "funcationA already defined in .h". What can I do?

Thanks

Edit: My production code was all part of one big project within Visual Studio. I decided to split the large project up into smaller components so I could unit test each component individually. This solved my problem as I could then fake off the dependencies for the unit I was testing without having multiple defines of the function.

wulfgarpro commented 2 years ago

@KasperZK can you provide a cut down example?

KasperZK commented 2 years ago

@wulfgarpro sure

#include "pch.h"
#include "CppUnitTest.h"
#include "..\fff.h"

extern "C"
{
    #include "ioDigitalInput.h"
}

DEFINE_FFF_GLOBALS;

FAKE_VALUE_FUNC(bool, ioFunc);

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTestfff
{
    TEST_CLASS(UnitTestfff)
    {
    public:

        TEST_METHOD(TestMethod1)
        {

        }
    };
}

ioFunc is implemented in ioDigitalInput.c

KasperZK commented 2 years ago

It should probably also be said that I am building the production code from another project within the same solution as the test project is placed in VS2019. The library files from the production code are then used in the test project.