chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
+1 vote

Hello,

is it possible to mock a function that returns a std::unique_ptr? The faked function shall return a nullptr. How can I do that?

example for the real function:

class A {

  public:

    std::unique_ptr<int> getIntPtr() { return 10;}

};

What I have tried but failed:

auto fakedObj = FAKE_ALL<A>();

WHEN_CALLED(fakedObj->getIntPtr()).Return(nullptr);

or

std::unique_ptr<int> nullPtr;

WHEN_CALLED(fakedObj->getIntPtr()).Return(nullPtr);

Thanks in advance!

asked by [KB] (610 points)

1 Answer

0 votes

Hi,

Unfortunately, with  std::unique_ptr we cannot directly support this due to it being by value on the stack and deleted, the current workaround - until we fix it is to use the PRIVATE API - PRIVATE_WHEN_CALLED(_, getIntPtr)...

You can find more about our PRIVATE API in this LINK.

Cheers,

Alon Sapozhnikov.

Support Speicalist.

answered by Alon_TypeMock (2.9k points)
...