Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#44 Mocking private member function, adding signature to MOCK_METHOD does not fix

closed
None
2014-02-08
2014-01-08
Bret Kuhns
No

I was attempting to mock a private function when I ran into the accessibility issue. The Turtle documentation states to explicitly state the signature in MOCK_METHOD's third parameter. When I do that (in Visual Studio 2012), I get the following warning:

warning C4002: too many actual parameters for macro 'MOCK_METHOD'

Followed by the inaccessble member error:

error C2248: 'MyClass::someFunctionName' : cannot access private member declared in class 'MyClass'

So, the suggestion in the documentation doesn't seem to fix the problem, and the three parameter variant of MOCK_METHOD causes the extra warning.

Luckily using MOCK_METHOD_EXT and including the signature and a custom identifier fixes the problem, but this is not the documented solution.

Discussion

1 2 > >> (Page 1 of 2)
  • Hi Bret,

    The following compiles fine with MSVC 2010

    struct base
    {
    private:
        virtual void m() = 0;
    };
    MOCK_BASE_CLASS( derived, base )
    {
        MOCK_METHOD( m, 0, void() )
    };
    

    Sadly I don't have MSVC 2012 available, but it seems for some reason MOCK_VARIADIC_MACROS doesn't get defined for that compiler.
    This is how it's done (in config.hpp):

    #if !defined(BOOST_NO_CXX11_VARIADIC_MACROS) && !defined(BOOST_NO_VARIADIC_MACROS)
    #   ifndef MOCK_NO_VARIADIC_MACROS
    #      define MOCK_VARIADIC_MACROS
    #   endif
    #endif
    

    Which version of boost are you using ?

    Thanks for reporting this !
    MAT.

     
  • Bret Kuhns
    Bret Kuhns
    2014-01-08

    I'm using boost 1.53. VS2012 (MSVC11) qualifies as an "additional test" compiler for 1.53.

     
    • I downloaded and tested with VS2012 Express and both boost 1.50 and 1.55 and the code compiles fine.
      I'll give it a go with boost 1.53 later today but I'm a bit at loss right now.
      I suppose you don't manually deactivate variadic macros?

      MAT.

       
  • It indeed compiles with boost 1.53.
    Would you be able to provide me with a test case ?

     
  • Bret Kuhns
    Bret Kuhns
    2014-01-14

    I'll try to come up with a bare minimum example and duplicate it on my dev machine as soon as I get a chance. I'll also try it on another machine and see if maybe it's a configuration issue. Thanks for investigating this on your end.

     
  • Bret Kuhns
    Bret Kuhns
    2014-01-29

    Sorry for the delay. I finally got a chance to test this on a separate machine. Visual Studio 2012 Ultimate Update 3. I copied and pasted your example above rather than my own and I get the same error message. I don't think express vs. ultimate edition should matter here.

    I'm attaching my example project so you can give it a try. You'll just need to add the include/lib paths for turtle and boost and it should compile enough to fail to compile main.cpp. If it compiles for you, then I'm very confused.

    I'm using turtle 1.2.4 with boost 1.53 (my team won't update boost until after this release).

     
    Attachments
    • Well, it does compile for me, but I might not use the exact versions, I'll double check.
      In the meanwhile what happens if you force this right before including turtle in your pch.h

      #define MOCK_VARIADIC_MACROS
      #include <turtle/mock.hpp>
      

      ?

      MAT.

       
  • Bret Kuhns
    Bret Kuhns
    2014-01-29

    I added MOCK_VARIADIC_MACROS before the include and still get the same errors. I pulled boost 1.55.0.16 and I get the same errors both with and without the macro.

     
    • I had to remove boost and turtle props files from the solution because MSVC was complaining about not finding them when loading the project file:

          <Import Project="..\..\..\..\..\..\..\dev\ThirdParty\turtle-1.2.4\turtle.props" />
          <Import Project="..\..\..\..\..\..\..\dev\ThirdParty\boost_1_53_0\boost.props" />
      

      Would you care to share them ? Maybe there's something in them which causes the issue ?

      MAT.

       
  • Bret Kuhns
    Bret Kuhns
    2014-01-30

    Sorry about that. They're very simple property sheets that I make so it's easy to add a third party library to my projects. I've attached them, though you'll find they simple add the include/lib paths for boost, and the include path for turtle. I drop them in the root directory of each library, respectively, then reference them in my projects that need them.

     
    Attachments
1 2 > >> (Page 1 of 2)