0

I try to isolate my class, so because of the exchanging external free functions with mock implementations, because of that, I have created my mock class for free functions, which had external dependency in another class. So how will I call this mock functions in my class?

Is there any implementation you can suggest to me?

I am thinking about creating a unique pointer and calling by it, but I am not really sure about this.

Which approach should I follow for doing it?

#include "source/common/main/dpc.hpp"
#include "source/common/main/dt.hpp"

#include <gmock/gmock.h>

namespace test {

namespace {

class dpcMock
{
public:
    MOCK_METHOD(bool, isVS, (const uint32_t id), ());

    MOCK_METHOD(St, gpType, (const uint32_t width), ());

    static dpcMock* activeMock;
};

} 
} 


#include "dpcMock.hpp"
#include "source/common/main/dpc.hpp"
#include <CppUTestExt/MockSupport.h>

test::dpcMock* test::dpcMock::activeMock = nullptr;

namespace common{
namespace main{
namespace {

bool isVS(const uint32_t id)
{
    if (::test::dpcMock::activeMock == nullptr)
    {
        return true;
    }
    return test::dpcMock::activeMock->isVS(id);
}

inline St gpType(const uint32_t width)
{
    if (::test::dpcMock::activeMock == nullptr)
    {
        return {};
    }
    return test::dpcMock::activeMock->gpType(width);
}

}
} 
} 

#include <memory>
#include <set>

#include "common/main/bm.hpp"
#include "common/main/ut/mocks/dpcMock.hpp"

namespace lucky{
namespace trap{
namespace main{

using namespace common::main;

class Dopy
{
public:
    void cDL(const sDL& sDLp);
    void cUL(const sUL& sULp);

    void csDL(const sDL& sDLp);
    void csUL(const sUL& sULp);

private:
    std::unique_ptr<sa> sa_fr;
    std::unique_ptr<bm> bm_fr;
};

} 
} 
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

IMO using unique_ptr is a way to go if you want to have one owner of this mock class, and the owner is the class under test. See this post which shows how to use unique_ptr is such context. However, for simplicity, I'd suggest to start of with regular references - it will be easier at the beginning.

Regarding your implementation: you need a base class for your mock and production class (best would be pure abstract class with no fields, just virtual methods) that your production and mock classes derive from (in the attached post, this is IBar). This interface is used in your class under test (Foo in post or Dopy in your code). Then you need real class (Bar in post or dpc in your code) that's for production code and mock class (BarMock, or dpcMock) that is used in tests. You need to use dependency injection, i.e. provide this pure abstract class to your class under test - real for production and mock for testing. In test env, you can set expectations on your mock and execute code on class under test.

At this point I don't see how dcp and Dopy are working together, but I think you should provide dcp to Dopy somehow (e.g. in ctor).

Quarra
  • 2,527
  • 1
  • 19
  • 27