HippoMocks:可以模拟非虚拟方法吗? [英] HippoMocks: is it possible to mock non-virtual methods?
问题描述
我已经开始使用HippoMocks编写单元测试.我想知道是否可以模拟非虚拟类方法吗?
I've starting using HippoMocks for writing unit tests. I would like to know if it's possible to mock non-virtual class methods?
首先看一下代码似乎表明该框架仅支持虚拟方法.但是,由于它支持对简单C函数的模拟,因此对于非虚拟类方法也应该可以做到这一点.
A first look at the code seems to indicate that the framework only supports virtual methods. But as it supports the mocking of simple C functions, it should be possible to do the same for non-virtual class methods.
有没有办法做到这一点?
Is there a way to achieve that?
推荐答案
这并非不可能,但这会导致非常奇怪的使用机制-或没有线程安全性的可能性.
It's not impossible, but it would lead to very weird use mechanisms - or no possibility for thread-safety.
C函数总是被完全模拟.在这种情况下,它总是重定向到模拟,您不能再调用原始模型了.
C functions are flat-out always mocked. In that case it's always redirecting to the mock, you cannot call the original anymore.
仅针对请求的对象模拟了C ++虚拟函数,其他任何对象在该处仍将具有常规函数.
C++ virtual functions are only mocked for the requested object, and any other object will still have the regular function there.
C ++非虚函数看起来像虚函数,但只能在每个类级别上模拟.很有可能您的编译器将内联这些函数,从而使其不太可能可靠.
C++ non-virtual functions would look like a virtual function, but only be mockable on a per-class level. It's also very likely that your compiler will inline these functions, making it not likely to be reliable.
我从某人那里得到了一个补丁,该补丁只是盲目地应用了,并且遭受了所描述的问题的困扰.您需要100%确保没有内联对该成员函数的任何访问,这几乎是不可能的.
I've had a patch from somebody that just applied it blindly, and it suffered from the described problems. You'd need to be 100% sure that any access to that member function is not inlined, which is just about impossible.
这篇关于HippoMocks:可以模拟非虚拟方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!