rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
802 stars 85 forks source link

Spy some dependencies #330

Closed jim-king-2000 closed 4 months ago

jim-king-2000 commented 5 months ago

Suppose we have a function in my production code.

void ProductionCode()
{
    do1Thing();
    do2Thing();
    do3Thing();
}

Now we need write a unit test for this function. And we want to spy do2Thing() and let it throw an exception. Pseudo-code is like this:

#include "ProductionCode.h"

TEST()
{
    mock("do2Thing", [](){ throw "exception"; });
    ProductionCode();
    mock.assertThrow("exception");
}

Is it possible to do this?

rollbear commented 5 months ago

I'm not sure. You may have to make your example a bit more detailed and explicit.

There are many things you can do, though. For example, you can use a local variable in the test, that keeps track of what has been seen, and a .LR_SIDE_EFFECT(...) that monitors/updates that variable, and maybe does something special depending on its value.

If you're asking if a mock can know that it's called from do2Thing(), then, probably not, depending on circumstances. You need a way to be able to identify do2Thing() in the call stack. Maybe it can be done by analyzing the result from std::stacktrace() if you're using C++23?

jim-king-2000 commented 5 months ago

Here is the pseudo-code which is similar to our production code in practice:

char *do1Thing()
{
    char *info = new char[64];
    // populate info with some important information
    return info;
}

void do2Things()
{
    // do some dangerous operations here
}

void do3Thing(char *info)
{
    // use info here
    delete info;
}

void ProductionCode()
{
    char *info = do1Thing();
    do2Thing();
    do3Thing(info);
}

In the code above, we create a dynamic memory in do1Thing() and we release it in do3Thing().

Now we need to write a unit test. The case is to "make sure all of the resources are released when any exception is thrown". So we need invoke ProductionCode() in our test code and let do2Thing() throw an exception. Obviously, in the production code, do2Thing() seldom fails. That is why we need mock it here.

#include "ProductionCode.h"

TEST()
{
    mock("do2Thing", [](){ throw "exception"; });
    ProductionCode();
    mock.assertThrow("exception");
}

Here we write a fake do2Thing and let it throw an exception. Then we call ProductionCode() and expect it could invoke the mocked version of do2Thing(). If it were true, we can see memory leak when running this case with valgrind or other memory debugger.

In other languages, such as node.js, we can do it like this. Every function including the system function could be mocked. Does this way work in C++? If no, what is the best practice?

jim-king-2000 commented 5 months ago

See the mock of node.js native test:

import { mock } from "node:test";

const tt = {
  f0() {
    console.log("f0");
    this.f1();
  },

  f1() {
    console.log("f1");
  },
};

mock.method(tt, "f1", () => console.log("mock"));
tt.f0();

The output is:

f0
mock

It means that tt.f1() has been successfully mocked. Is there any mechanism similar to the one of node.js?