简体   繁体   中英

HippoMocks: is it possible to mock non-virtual methods?

I've starting using HippoMocks for writing unit tests. I would like to know if it's possible to mock non-virtual class methods?

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 functions are flat-out always mocked. In that case it's always redirecting to the mock, you cannot call the original anymore.

C++ virtual functions are only mocked for the requested object, and any other object will still have the regular function there.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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