Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4086524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:44:19+00:00 2026-05-20T18:44:19+00:00

I am using google mock library in my unit tests, and I am trying

  • 0

I am using google mock library in my unit tests, and I am trying to do a custom check that can fail.

Next example demonstrates what I am trying to do :

struct Base
{
};
struct Derived : Base
{
  int a;
};

struct MockClass
{
  MOCK_METHOD1( Send, void ( Base & ) );
};

Now I would like to check if the fake object got passed object of type Derived in the Send method, and the value a.
So, how to do it?

My idea is to use Invoke and forward the call to some function, which will dynamic_cast from Base to Derived, and check the value. If the type is not of expected throw an exception. Like this :

void TestCall( Base &obj )
{
  Derived *realObj = dynamic_cast< Derived * >( &obj );
  if ( NULL == realObj )
  {
    throw 123;
  }
}

then test like this :

MockClass mockObj;
EXPECT_CALL( mockObj, Send(_) )
  .WillOnce( Invoke( &TestCall ) );

Is this going to work? Or is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:44:20+00:00Added an answer on May 20, 2026 at 6:44 pm

    You can define a custom matcher to verify the type and the value of your argument simultaneously:

    MATCHER_P(IsDerivedAnEqual, a, "") {
      Derived* derived_arg = dynamic_cast<Derived*>(&arg);
      return derived_arg != NULL && derived_arg->a == a;
    }
    
    EXPECT_CALL(mock_obj, Send(IsDerivedAndEqual(5));
    

    You can also use composite matchers to build more complex conditions.

    Calls you put into the WillOnce() expression are actions. They are only invoked if a call matches the expectations you have set and are supposed to mimic what an external dependency would do if invoked. Using them to set expectations will not work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Google Test and Google Mock frameworks for a project's unit tests. I
I'm trying to make google mock but I'm only getting one library: gmock_main.a Why
I am using Google Mock 1.6 RC and am trying to Mock a COM
I've started using google js test and have become stuck trying to unit test
In our project we are using Google Mock, but in multiple places we are
Using Google App Engine, I am trying to urlfetch a gzip file from a
Using google, yahoo and AOL smtp and PHPMailer how many mails can be sent
I'm using the Boost::Test library for unit testing, and I've in general been hacking
Using google docs, a colleague has created a plot similar to: I'm trying to
I am trying to figure out how can I mock (simply) current_user inside my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.