简体   繁体   English

对共享库进行单元测试时,如何用模拟实现替换函数?

[英]How do I replace a function by a mock implementation when unit-testing a shared library?

I am having the opposite problem that Can you compile a shared object to prefer local symbols even if it's being loaded by a program compiled with -rdynamic?我遇到了相反的问题, 即使它是由使用 -rdynamic 编译的程序加载的,您能否编译共享对象以更喜欢本地符号? is solving.正在解决。

Using the naming from the linked question, I have a dynamic library where baz calls bar , and I have a test binary exercising the library, which substitutes its own fake implementation of bar for test purposes.使用链接问题中的命名,我有一个动态库,其中baz调用bar ,并且我有一个测试二进制文件来运行该库,它代替自己的bar假实现来进行测试。 This works fine on Linux, because -rdynamic is used to link.这在 Linux 上运行良好,因为-rdynamic用于链接。

The source of the test is https://github.com/apache/qpid-dispatch/blob/b172f501028b36d786b4c83bcee1e195cd17fcf2/tests/timer_test.c .测试来源为https://github.com/apache/qpid-dispatch/blob/b172f501028b36d786b4c83bcee1e195cd17fcf2/tests/timer_test.c The functions being mocked are, among others qd_server_timeout and qd_timer_now (that is the inlined one, see comments).被模拟的函数包括qd_server_timeoutqd_timer_now (这是内联的,见评论)。

I am at a loss how to achieve the same on macOS.我不知道如何在 macOS 上实现相同的目标。 What are the correct linker options there?那里正确的链接器选项是什么?

For now, it is resolved by adding __attribute__((noinline)) to bar ( commit ), and linking with -Wl,-flat_namespace ( commit ).现在,它是解决通过添加__attribute__((noinline))bar提交),并与联-Wl,-flat_namespace提交)。 The -export_dynamic was actually not needed.实际上不需要-export_dynamic

I am still looking for alternative solutions.我仍在寻找替代解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM