Comments (3)
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?
from trompeloeil.
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?
from trompeloeil.
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?
from trompeloeil.
Related Issues (20)
- How can i mock a member function of A class in a B class's function? HOT 3
- CMake version required not correct HOT 5
- Setting expectations in a function fails HOT 6
- how can I mock the static member function and non-virtual member fuctions? HOT 2
- Problematic traits for ``streamer`` primary template HOT 4
- Documentation, typo and missing bits and bobs HOT 3
- Compile error when returning values from a tl::expected with a void expected value HOT 5
- Refactoring with sequenced expectations HOT 2
- Build for QNX tests fail due to catch2 header file HOT 1
- Wrong Expectaition message HOT 1
- [Bug] Fails to run tests with catch 3.x HOT 4
- Tests fail to compile: error: 'ret_count' is an unsafe buffer that does not perform bounds checks HOT 4
- Expect call returning a reference HOT 2
- can this cppcheck inline suppression be added HOT 1
- unreal engine support & noreturn/std::abort HOT 3
- Setting side effects after creating expectactions (inside DOCTEST_SUBCASEs) HOT 5
- Feature Request: Allow specifying calling convention attributes to Mock methods HOT 12
- ``trompeloeil_movable_mock`` only provides move-ctor HOT 3
- Warnings with gcc's -Weffc++ HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trompeloeil.