Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i mock nonvirtual member function? #32

Open
git45016683 opened this issue Apr 2, 2022 · 4 comments
Open

How can i mock nonvirtual member function? #32

git45016683 opened this issue Apr 2, 2022 · 4 comments

Comments

@git45016683
Copy link

git45016683 commented Apr 2, 2022

i try to mock a nonvirtual member function, but it show me error like this:

unknown file: Failure
C++ exception with description "Virtual method address should be odd, please make sure the method long (Cfoobar::*)(long) is a virtual method" thrown in the test body.

i want to know below information:

  1. Is mockcpp support mock nonvirtual member function?
  2. if it's support, how can it do that?
// sample code
// mock target class---want to mock nonvirtual function: foobar_l
class Cfoobar {
public:
    Cfoobar() {}
    virtual ~Cfoobar() {}
    long foobar_l(long x) {return x;}
};
// unit test for this class
class CcallCfoobarByobj {
public:
    CcallCfoobarByobj() {}
    virtual ~CcallCfoobarByobj() {}
    Cfoobar cfoobar;
    long callfoobar_l(long x) {return cfoobar.foobar_l(x);}
};
// unit test case
TEST(verifyMockcpp, memberFunctionByObj_Mock) {
    MockObject<Cfoobar> mocker;
    MOCK_METHOD(mocker, foobar_l).stubs().will(returnValue(36));

    CcallCfoobarByobj obj;
    EXPECT_EQ(obj.callfoobar_l(2), 36);
    mocker.verify();
    EXPECT_EQ(obj.callfoobar_l(2), 2);
}
@sinojelly
Copy link
Owner

You can take a look at this:
https://github.com/sinojelly/mockcpp/blob/master/tests/ut/TestNonvirtualMethodMocker.h

Because nonvirtual member function is not interface, maybe mutable, tests depend on it is harmful.
So there is no convenient interface to mock nonvirtual member function, just a demo there.

@git45016683
Copy link
Author

Thanks for your response and help.
i try to do like the link, but got error
"
error: ‘CApiHookFunctor’ was not declared in this scope
(const void*)CApiHookFunctor<FAKE_BOOST_TYPEOF(Cfoobar::fooWORDref_static)>::hook,
^
"
and the same error with 'ThunkCodeProvider'

i search the mockcpp resposity, but didn't found key word 'ThunkCodeProvider' and 'CApiHookFunctor' except this file"https://github.com/sinojelly/mockcpp/blob/master/tests/ut/TestNonvirtualMethodMocker.h"

May I ignore something important? or what's wrong happed?

@jhjhuaijie
Copy link

jhjhuaijie commented Sep 20, 2022

Does mockcpp support virtual functions? It is not pure virtual functions.
I try to mock virtual function in class, but return value is not what I expected
thank you for your reply.

@sinojelly
Copy link
Owner

Does mockcpp support virtual functions? It is not pure virtual functions. I try to mock virtual function in class, but return value is not what I expected thank you for your reply.

I think it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants