Using Google Mock outside of Google Test

14 May, 2009 § 8 Comments

I’ve just finished getting an implementation going of MSTest with GMock, and I wanted to document all things neccessary to use GMock in a testing framework other than GTest.

To get GMock to build in VS2008, I had to install the Visual Studio 2008 Feature Pack. This was so I could get full TR1 support, specifically std::tr1::tuple. Other coworkers of mine haven’t needed to install the Feature Pack, though I presume that they didn’t need to because they had SP1 installed (which I thought I had installed). After installing the Feature Pack, I then had to reinstall SP1 due to some odd compiler errors that were occurring due to mismatched compiler versions.

Now some quick gotchas with GMock:

  • If you use the included gmock_gen.py script to generate your mocks from your pure abstract classes, then you will need to make sure that there are no default arguments in any of your mock’s methods.
  • When you add expectations to a mock, you should call VerifyAndClearExpectations at the end of your test method. You should assert that this method returns True. If you don’t include this method call, then your testing framework won’t be able to catch the GTest exceptions that will get thrown if the GMock’s expectations aren’t met.

Those are the only points I wanted to put out there at this point. When I run in to more bumps in the road I’ll be sure to publish them.

Tagged: , , ,

§ 8 Responses to Using Google Mock outside of Google Test

  • dripfeed says:

    Hi Jaws

    How did you actually integrate GMock with Visual Studio 2008? We’re using VS2008’s in-built MSTest functionality to drive our C++ unit tests, but how do I integrate Googlemock into it too ?

    Thanks for any help.

    • msujaws says:

      I’ll be glad to help you. To add GMock to my MSTest project, I added the google mock VC++ project (that is within the downloaded zip file from Google) to the solution. I then added a project dependency of GMock to the MSTest project. From there, you should be able to just #include and use Google mock’s functionality. If you have more questions or if I left something out, please let me know.

  • dripfeed says:

    Thanks for that msujaws. I’ll try that. :-)

  • dripfeed says:

    Hi again Jared

    Using GMock as easily as you suggest is still beyond me. See this Googlemock discussion thread for further info:

    http://groups.google.com/group/googlemock/browse_frm/thread/c0b1198fe3feda78/523f0f05100f4c3d#523f0f05100f4c3d

    Have you conquered this problem? Maybe you didn’t need to because you structure your production code, test code and mocks a certain way?

    Thanks for any advice.

  • msujaws says:

    Hi,

    Thanks for pointing me to that question. The problem deals with the way that Sean has the class declared. This is how he has it declared:

    // IBankAccount.h
    #ifdef MY_API
    #define MY_API __declspec(dllexport)
    #else
    #define MY_API __declspec(dllimport)
    #endif

    class MY_API IBankAccount {
    public:
    virtual bool VerifyCustomerPin(long pin) = 0;
    };

    They way that the descspec(dllexport) works is that it will try to export the whole class, including the compiler generated compiler and destructor.

    To fix this, you would need to move the macro MY_API from the class declaration to the function declaration, like so:

    class IBankAccount {
    public:
    MY_API virtual bool VerifyCustomerPin(long pin) = 0;
    };

    You will need to do this for all exported methods.

  • Nils says:

    Hi jaws,

    how do you translate the exceptions. I only receive SEHExceptions for the expectations in my managed code test:

    e.g.:
    MockObserver* const observer = new MockObserver();

    EXPECT_CALL(*observer, OnConfigChanged()).Times(AtLeast(3));

    observer->OnConfigChanged();
    observer->OnConfigChanged();

    delete observer;

    This results in:
    System.Runtime.InteropServices.SEHException: External component has thrown an exception

    Thanks for any help…

  • msujaws says:

    Hi Nils,

    SEHExceptions likely can’t be translated to C++ exceptions. I have not seen a way to do that.

    Are you expecting an exception to be thrown by your test? If you would like to find out what exception is being thrown, you could wrap your test in a __try and follow that by a __catch.

    I’m not sure how to use Google Mock to catch expected SEH exceptions through the framework, but you can always write your test as follows:

    bool expectedExceptionCaught = false;
    __try {
    MockObserver* const observer = new MockObserver;

    EXPECT_CALL(*observer, OnConfigChanged()).Times(AtLeast(3));

    observer->OnConfigChanged();
    observer->OnConfigChanged();

    delete observer;
    }
    __catch( ExpectedExceptionType& exception )
    {
    expectedExceptionCaught = true;
    }
    __catch( … )
    {
    Assert::Fail();
    }

    if ( !expectedExceptionCaught )
    {
    Assert::Fail();
    }

    Also, do you know at what line the exception is being thrown?

  • [...] Using Google Mock outside of Google Test May 2009 7 comments [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Using Google Mock outside of Google Test at JAWS.

meta

Follow

Get every new post delivered to your Inbox.

Join 998 other followers