Blame googlemock/docs/FrequentlyAskedQuestions.md

Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
Please send your questions to the
Packit bd1cd8
[googlemock](http://groups.google.com/group/googlemock) discussion
Packit bd1cd8
group. If you need help with compiler errors, make sure you have
Packit bd1cd8
tried [Google Mock Doctor](#How_am_I_supposed_to_make_sense_of_these_horrible_template_error.md) first.
Packit bd1cd8
Packit bd1cd8
## When I call a method on my mock object, the method for the real object is invoked instead.  What's the problem? ##
Packit bd1cd8
Packit bd1cd8
In order for a method to be mocked, it must be _virtual_, unless you use the [high-perf dependency injection technique](CookBook.md#mocking-nonvirtual-methods).
Packit bd1cd8
Packit bd1cd8
## I wrote some matchers.  After I upgraded to a new version of Google Mock, they no longer compile.  What's going on? ##
Packit bd1cd8
Packit bd1cd8
After version 1.4.0 of Google Mock was released, we had an idea on how
Packit bd1cd8
to make it easier to write matchers that can generate informative
Packit bd1cd8
messages efficiently.  We experimented with this idea and liked what
Packit bd1cd8
we saw.  Therefore we decided to implement it.
Packit bd1cd8
Packit bd1cd8
Unfortunately, this means that if you have defined your own matchers
Packit bd1cd8
by implementing `MatcherInterface` or using `MakePolymorphicMatcher()`,
Packit bd1cd8
your definitions will no longer compile.  Matchers defined using the
Packit bd1cd8
`MATCHER*` family of macros are not affected.
Packit bd1cd8
Packit bd1cd8
Sorry for the hassle if your matchers are affected.  We believe it's
Packit bd1cd8
in everyone's long-term interest to make this change sooner than
Packit bd1cd8
later.  Fortunately, it's usually not hard to migrate an existing
Packit bd1cd8
matcher to the new API.  Here's what you need to do:
Packit bd1cd8
Packit bd1cd8
If you wrote your matcher like this:
Packit bd1cd8
```
Packit bd1cd8
// Old matcher definition that doesn't work with the latest
Packit bd1cd8
// Google Mock.
Packit bd1cd8
using ::testing::MatcherInterface;
Packit bd1cd8
...
Packit bd1cd8
class MyWonderfulMatcher : public MatcherInterface<MyType> {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  virtual bool Matches(MyType value) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    return value.GetFoo() > 5;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you'll need to change it to:
Packit bd1cd8
```
Packit bd1cd8
// New matcher definition that works with the latest Google Mock.
Packit bd1cd8
using ::testing::MatcherInterface;
Packit bd1cd8
using ::testing::MatchResultListener;
Packit bd1cd8
...
Packit bd1cd8
class MyWonderfulMatcher : public MatcherInterface<MyType> {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  virtual bool MatchAndExplain(MyType value,
Packit bd1cd8
                               MatchResultListener* listener) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    return value.GetFoo() > 5;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
(i.e. rename `Matches()` to `MatchAndExplain()` and give it a second
Packit bd1cd8
argument of type `MatchResultListener*`.)
Packit bd1cd8
Packit bd1cd8
If you were also using `ExplainMatchResultTo()` to improve the matcher
Packit bd1cd8
message:
Packit bd1cd8
```
Packit bd1cd8
// Old matcher definition that doesn't work with the lastest
Packit bd1cd8
// Google Mock.
Packit bd1cd8
using ::testing::MatcherInterface;
Packit bd1cd8
...
Packit bd1cd8
class MyWonderfulMatcher : public MatcherInterface<MyType> {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  virtual bool Matches(MyType value) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    return value.GetFoo() > 5;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  virtual void ExplainMatchResultTo(MyType value,
Packit bd1cd8
                                    ::std::ostream* os) const {
Packit bd1cd8
    // Prints some helpful information to os to help
Packit bd1cd8
    // a user understand why value matches (or doesn't match).
Packit bd1cd8
    *os << "the Foo property is " << value.GetFoo();
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you should move the logic of `ExplainMatchResultTo()` into
Packit bd1cd8
`MatchAndExplain()`, using the `MatchResultListener` argument where
Packit bd1cd8
the `::std::ostream` was used:
Packit bd1cd8
```
Packit bd1cd8
// New matcher definition that works with the latest Google Mock.
Packit bd1cd8
using ::testing::MatcherInterface;
Packit bd1cd8
using ::testing::MatchResultListener;
Packit bd1cd8
...
Packit bd1cd8
class MyWonderfulMatcher : public MatcherInterface<MyType> {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  virtual bool MatchAndExplain(MyType value,
Packit bd1cd8
                               MatchResultListener* listener) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    *listener << "the Foo property is " << value.GetFoo();
Packit bd1cd8
    return value.GetFoo() > 5;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
If your matcher is defined using `MakePolymorphicMatcher()`:
Packit bd1cd8
```
Packit bd1cd8
// Old matcher definition that doesn't work with the latest
Packit bd1cd8
// Google Mock.
Packit bd1cd8
using ::testing::MakePolymorphicMatcher;
Packit bd1cd8
...
Packit bd1cd8
class MyGreatMatcher {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  bool Matches(MyType value) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    return value.GetBar() < 42;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
... MakePolymorphicMatcher(MyGreatMatcher()) ...
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you should rename the `Matches()` method to `MatchAndExplain()` and
Packit bd1cd8
add a `MatchResultListener*` argument (the same as what you need to do
Packit bd1cd8
for matchers defined by implementing `MatcherInterface`):
Packit bd1cd8
```
Packit bd1cd8
// New matcher definition that works with the latest Google Mock.
Packit bd1cd8
using ::testing::MakePolymorphicMatcher;
Packit bd1cd8
using ::testing::MatchResultListener;
Packit bd1cd8
...
Packit bd1cd8
class MyGreatMatcher {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  bool MatchAndExplain(MyType value,
Packit bd1cd8
                       MatchResultListener* listener) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    return value.GetBar() < 42;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
... MakePolymorphicMatcher(MyGreatMatcher()) ...
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
If your polymorphic matcher uses `ExplainMatchResultTo()` for better
Packit bd1cd8
failure messages:
Packit bd1cd8
```
Packit bd1cd8
// Old matcher definition that doesn't work with the latest
Packit bd1cd8
// Google Mock.
Packit bd1cd8
using ::testing::MakePolymorphicMatcher;
Packit bd1cd8
...
Packit bd1cd8
class MyGreatMatcher {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  bool Matches(MyType value) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    return value.GetBar() < 42;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
void ExplainMatchResultTo(const MyGreatMatcher& matcher,
Packit bd1cd8
                          MyType value,
Packit bd1cd8
                          ::std::ostream* os) {
Packit bd1cd8
  // Prints some helpful information to os to help
Packit bd1cd8
  // a user understand why value matches (or doesn't match).
Packit bd1cd8
  *os << "the Bar property is " << value.GetBar();
Packit bd1cd8
}
Packit bd1cd8
... MakePolymorphicMatcher(MyGreatMatcher()) ...
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you'll need to move the logic inside `ExplainMatchResultTo()` to
Packit bd1cd8
`MatchAndExplain()`:
Packit bd1cd8
```
Packit bd1cd8
// New matcher definition that works with the latest Google Mock.
Packit bd1cd8
using ::testing::MakePolymorphicMatcher;
Packit bd1cd8
using ::testing::MatchResultListener;
Packit bd1cd8
...
Packit bd1cd8
class MyGreatMatcher {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  bool MatchAndExplain(MyType value,
Packit bd1cd8
                       MatchResultListener* listener) const {
Packit bd1cd8
    // Returns true if value matches.
Packit bd1cd8
    *listener << "the Bar property is " << value.GetBar();
Packit bd1cd8
    return value.GetBar() < 42;
Packit bd1cd8
  }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
... MakePolymorphicMatcher(MyGreatMatcher()) ...
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
For more information, you can read these
Packit bd1cd8
[two](CookBook.md#writing-new-monomorphic-matchers)
Packit bd1cd8
[recipes](CookBook.md#writing-new-polymorphic-matchers)
Packit bd1cd8
from the cookbook.  As always, you
Packit bd1cd8
are welcome to post questions on `googlemock@googlegroups.com` if you
Packit bd1cd8
need any help.
Packit bd1cd8
Packit bd1cd8
## When using Google Mock, do I have to use Google Test as the testing framework?  I have my favorite testing framework and don't want to switch. ##
Packit bd1cd8
Packit bd1cd8
Google Mock works out of the box with Google Test.  However, it's easy
Packit bd1cd8
to configure it to work with any testing framework of your choice.
Packit bd1cd8
[Here](ForDummies.md#using-google-mock-with-any-testing-framework) is how.
Packit bd1cd8
Packit bd1cd8
## How am I supposed to make sense of these horrible template errors? ##
Packit bd1cd8
Packit bd1cd8
If you are confused by the compiler errors gcc threw at you,
Packit bd1cd8
try consulting the _Google Mock Doctor_ tool first.  What it does is to
Packit bd1cd8
scan stdin for gcc error messages, and spit out diagnoses on the
Packit bd1cd8
problems (we call them diseases) your code has.
Packit bd1cd8
Packit bd1cd8
To "install", run command:
Packit bd1cd8
```
Packit bd1cd8
alias gmd='<path to googlemock>/scripts/gmock_doctor.py'
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
To use it, do:
Packit bd1cd8
```
Packit bd1cd8
<your-favorite-build-command> <your-test> 2>&1 | gmd
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
For example:
Packit bd1cd8
```
Packit bd1cd8
make my_test 2>&1 | gmd
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Or you can run `gmd` and copy-n-paste gcc's error messages to it.
Packit bd1cd8
Packit bd1cd8
## Can I mock a variadic function? ##
Packit bd1cd8
Packit bd1cd8
You cannot mock a variadic function (i.e. a function taking ellipsis
Packit bd1cd8
(`...`) arguments) directly in Google Mock.
Packit bd1cd8
Packit bd1cd8
The problem is that in general, there is _no way_ for a mock object to
Packit bd1cd8
know how many arguments are passed to the variadic method, and what
Packit bd1cd8
the arguments' types are.  Only the _author of the base class_ knows
Packit bd1cd8
the protocol, and we cannot look into his head.
Packit bd1cd8
Packit bd1cd8
Therefore, to mock such a function, the _user_ must teach the mock
Packit bd1cd8
object how to figure out the number of arguments and their types.  One
Packit bd1cd8
way to do it is to provide overloaded versions of the function.
Packit bd1cd8
Packit bd1cd8
Ellipsis arguments are inherited from C and not really a C++ feature.
Packit bd1cd8
They are unsafe to use and don't work with arguments that have
Packit bd1cd8
constructors or destructors.  Therefore we recommend to avoid them in
Packit bd1cd8
C++ as much as possible.
Packit bd1cd8
Packit bd1cd8
## MSVC gives me warning C4301 or C4373 when I define a mock method with a const parameter.  Why? ##
Packit bd1cd8
Packit bd1cd8
If you compile this using Microsoft Visual C++ 2005 SP1:
Packit bd1cd8
```
Packit bd1cd8
class Foo {
Packit bd1cd8
  ...
Packit bd1cd8
  virtual void Bar(const int i) = 0;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
class MockFoo : public Foo {
Packit bd1cd8
  ...
Packit bd1cd8
  MOCK_METHOD1(Bar, void(const int i));
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
You may get the following warning:
Packit bd1cd8
```
Packit bd1cd8
warning C4301: 'MockFoo::Bar': overriding virtual function only differs from 'Foo::Bar' by const/volatile qualifier
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
This is a MSVC bug.  The same code compiles fine with gcc ,for
Packit bd1cd8
example.  If you use Visual C++ 2008 SP1, you would get the warning:
Packit bd1cd8
```
Packit bd1cd8
warning C4373: 'MockFoo::Bar': virtual function overrides 'Foo::Bar', previous versions of the compiler did not override when parameters only differed by const/volatile qualifiers
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
In C++, if you _declare_ a function with a `const` parameter, the
Packit bd1cd8
`const` modifier is _ignored_.  Therefore, the `Foo` base class above
Packit bd1cd8
is equivalent to:
Packit bd1cd8
```
Packit bd1cd8
class Foo {
Packit bd1cd8
  ...
Packit bd1cd8
  virtual void Bar(int i) = 0;  // int or const int?  Makes no difference.
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
In fact, you can _declare_ Bar() with an `int` parameter, and _define_
Packit bd1cd8
it with a `const int` parameter.  The compiler will still match them
Packit bd1cd8
up.
Packit bd1cd8
Packit bd1cd8
Since making a parameter `const` is meaningless in the method
Packit bd1cd8
_declaration_, we recommend to remove it in both `Foo` and `MockFoo`.
Packit bd1cd8
That should workaround the VC bug.
Packit bd1cd8
Packit bd1cd8
Note that we are talking about the _top-level_ `const` modifier here.
Packit bd1cd8
If the function parameter is passed by pointer or reference, declaring
Packit bd1cd8
the _pointee_ or _referee_ as `const` is still meaningful.  For
Packit bd1cd8
example, the following two declarations are _not_ equivalent:
Packit bd1cd8
```
Packit bd1cd8
void Bar(int* p);        // Neither p nor *p is const.
Packit bd1cd8
void Bar(const int* p);  // p is not const, but *p is.
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## I have a huge mock class, and Microsoft Visual C++ runs out of memory when compiling it.  What can I do? ##
Packit bd1cd8
Packit bd1cd8
We've noticed that when the `/clr` compiler flag is used, Visual C++
Packit bd1cd8
uses 5~6 times as much memory when compiling a mock class.  We suggest
Packit bd1cd8
to avoid `/clr` when compiling native C++ mocks.
Packit bd1cd8
Packit bd1cd8
## I can't figure out why Google Mock thinks my expectations are not satisfied.  What should I do? ##
Packit bd1cd8
Packit bd1cd8
You might want to run your test with
Packit bd1cd8
`--gmock_verbose=info`.  This flag lets Google Mock print a trace
Packit bd1cd8
of every mock function call it receives.  By studying the trace,
Packit bd1cd8
you'll gain insights on why the expectations you set are not met.
Packit bd1cd8
Packit bd1cd8
## How can I assert that a function is NEVER called? ##
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
EXPECT_CALL(foo, Bar(_))
Packit bd1cd8
    .Times(0);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## I have a failed test where Google Mock tells me TWICE that a particular expectation is not satisfied.  Isn't this redundant? ##
Packit bd1cd8
Packit bd1cd8
When Google Mock detects a failure, it prints relevant information
Packit bd1cd8
(the mock function arguments, the state of relevant expectations, and
Packit bd1cd8
etc) to help the user debug.  If another failure is detected, Google
Packit bd1cd8
Mock will do the same, including printing the state of relevant
Packit bd1cd8
expectations.
Packit bd1cd8
Packit bd1cd8
Sometimes an expectation's state didn't change between two failures,
Packit bd1cd8
and you'll see the same description of the state twice.  They are
Packit bd1cd8
however _not_ redundant, as they refer to _different points in time_.
Packit bd1cd8
The fact they are the same _is_ interesting information.
Packit bd1cd8
Packit bd1cd8
## I get a heap check failure when using a mock object, but using a real object is fine.  What can be wrong? ##
Packit bd1cd8
Packit bd1cd8
Does the class (hopefully a pure interface) you are mocking have a
Packit bd1cd8
virtual destructor?
Packit bd1cd8
Packit bd1cd8
Whenever you derive from a base class, make sure its destructor is
Packit bd1cd8
virtual.  Otherwise Bad Things will happen.  Consider the following
Packit bd1cd8
code:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
class Base {
Packit bd1cd8
 public:
Packit bd1cd8
  // Not virtual, but should be.
Packit bd1cd8
  ~Base() { ... }
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
class Derived : public Base {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
 private:
Packit bd1cd8
  std::string value_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
...
Packit bd1cd8
  Base* p = new Derived;
Packit bd1cd8
  ...
Packit bd1cd8
  delete p;  // Surprise! ~Base() will be called, but ~Derived() will not
Packit bd1cd8
             // - value_ is leaked.
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
By changing `~Base()` to virtual, `~Derived()` will be correctly
Packit bd1cd8
called when `delete p` is executed, and the heap checker
Packit bd1cd8
will be happy.
Packit bd1cd8
Packit bd1cd8
## The "newer expectations override older ones" rule makes writing expectations awkward.  Why does Google Mock do that? ##
Packit bd1cd8
Packit bd1cd8
When people complain about this, often they are referring to code like:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
// foo.Bar() should be called twice, return 1 the first time, and return
Packit bd1cd8
// 2 the second time.  However, I have to write the expectations in the
Packit bd1cd8
// reverse order.  This sucks big time!!!
Packit bd1cd8
EXPECT_CALL(foo, Bar())
Packit bd1cd8
    .WillOnce(Return(2))
Packit bd1cd8
    .RetiresOnSaturation();
Packit bd1cd8
EXPECT_CALL(foo, Bar())
Packit bd1cd8
    .WillOnce(Return(1))
Packit bd1cd8
    .RetiresOnSaturation();
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
The problem is that they didn't pick the **best** way to express the test's
Packit bd1cd8
intent.
Packit bd1cd8
Packit bd1cd8
By default, expectations don't have to be matched in _any_ particular
Packit bd1cd8
order.  If you want them to match in a certain order, you need to be
Packit bd1cd8
explicit.  This is Google Mock's (and jMock's) fundamental philosophy: it's
Packit bd1cd8
easy to accidentally over-specify your tests, and we want to make it
Packit bd1cd8
harder to do so.
Packit bd1cd8
Packit bd1cd8
There are two better ways to write the test spec.  You could either
Packit bd1cd8
put the expectations in sequence:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
// foo.Bar() should be called twice, return 1 the first time, and return
Packit bd1cd8
// 2 the second time.  Using a sequence, we can write the expectations
Packit bd1cd8
// in their natural order.
Packit bd1cd8
{
Packit bd1cd8
  InSequence s;
Packit bd1cd8
  EXPECT_CALL(foo, Bar())
Packit bd1cd8
      .WillOnce(Return(1))
Packit bd1cd8
      .RetiresOnSaturation();
Packit bd1cd8
  EXPECT_CALL(foo, Bar())
Packit bd1cd8
      .WillOnce(Return(2))
Packit bd1cd8
      .RetiresOnSaturation();
Packit bd1cd8
}
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
or you can put the sequence of actions in the same expectation:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
// foo.Bar() should be called twice, return 1 the first time, and return
Packit bd1cd8
// 2 the second time.
Packit bd1cd8
EXPECT_CALL(foo, Bar())
Packit bd1cd8
    .WillOnce(Return(1))
Packit bd1cd8
    .WillOnce(Return(2))
Packit bd1cd8
    .RetiresOnSaturation();
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Back to the original questions: why does Google Mock search the
Packit bd1cd8
expectations (and `ON_CALL`s) from back to front?  Because this
Packit bd1cd8
allows a user to set up a mock's behavior for the common case early
Packit bd1cd8
(e.g. in the mock's constructor or the test fixture's set-up phase)
Packit bd1cd8
and customize it with more specific rules later.  If Google Mock
Packit bd1cd8
searches from front to back, this very useful pattern won't be
Packit bd1cd8
possible.
Packit bd1cd8
Packit bd1cd8
## Google Mock prints a warning when a function without EXPECT\_CALL is called, even if I have set its behavior using ON\_CALL.  Would it be reasonable not to show the warning in this case? ##
Packit bd1cd8
Packit bd1cd8
When choosing between being neat and being safe, we lean toward the
Packit bd1cd8
latter.  So the answer is that we think it's better to show the
Packit bd1cd8
warning.
Packit bd1cd8
Packit bd1cd8
Often people write `ON_CALL`s in the mock object's
Packit bd1cd8
constructor or `SetUp()`, as the default behavior rarely changes from
Packit bd1cd8
test to test.  Then in the test body they set the expectations, which
Packit bd1cd8
are often different for each test.  Having an `ON_CALL` in the set-up
Packit bd1cd8
part of a test doesn't mean that the calls are expected.  If there's
Packit bd1cd8
no `EXPECT_CALL` and the method is called, it's possibly an error.  If
Packit bd1cd8
we quietly let the call go through without notifying the user, bugs
Packit bd1cd8
may creep in unnoticed.
Packit bd1cd8
Packit bd1cd8
If, however, you are sure that the calls are OK, you can write
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
EXPECT_CALL(foo, Bar(_))
Packit bd1cd8
    .WillRepeatedly(...);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
instead of
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
ON_CALL(foo, Bar(_))
Packit bd1cd8
    .WillByDefault(...);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
This tells Google Mock that you do expect the calls and no warning should be
Packit bd1cd8
printed.
Packit bd1cd8
Packit bd1cd8
Also, you can control the verbosity using the `--gmock_verbose` flag.
Packit bd1cd8
If you find the output too noisy when debugging, just choose a less
Packit bd1cd8
verbose level.
Packit bd1cd8
Packit bd1cd8
## How can I delete the mock function's argument in an action? ##
Packit bd1cd8
Packit bd1cd8
If you find yourself needing to perform some action that's not
Packit bd1cd8
supported by Google Mock directly, remember that you can define your own
Packit bd1cd8
actions using
Packit bd1cd8
[MakeAction()](CookBook.md#writing-new-actions) or
Packit bd1cd8
[MakePolymorphicAction()](CookBook.md#writing_new_polymorphic_actions),
Packit bd1cd8
or you can write a stub function and invoke it using
Packit bd1cd8
[Invoke()](CookBook.md#using-functions_methods_functors).
Packit bd1cd8
Packit bd1cd8
## MOCK\_METHODn()'s second argument looks funny.  Why don't you use the MOCK\_METHODn(Method, return\_type, arg\_1, ..., arg\_n) syntax? ##
Packit bd1cd8
Packit bd1cd8
What?!  I think it's beautiful. :-)
Packit bd1cd8
Packit bd1cd8
While which syntax looks more natural is a subjective matter to some
Packit bd1cd8
extent, Google Mock's syntax was chosen for several practical advantages it
Packit bd1cd8
has.
Packit bd1cd8
Packit bd1cd8
Try to mock a function that takes a map as an argument:
Packit bd1cd8
```
Packit bd1cd8
virtual int GetSize(const map<int, std::string>& m);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Using the proposed syntax, it would be:
Packit bd1cd8
```
Packit bd1cd8
MOCK_METHOD1(GetSize, int, const map<int, std::string>& m);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Guess what?  You'll get a compiler error as the compiler thinks that
Packit bd1cd8
`const map<int, std::string>& m` are **two**, not one, arguments. To work
Packit bd1cd8
around this you can use `typedef` to give the map type a name, but
Packit bd1cd8
that gets in the way of your work.  Google Mock's syntax avoids this
Packit bd1cd8
problem as the function's argument types are protected inside a pair
Packit bd1cd8
of parentheses:
Packit bd1cd8
```
Packit bd1cd8
// This compiles fine.
Packit bd1cd8
MOCK_METHOD1(GetSize, int(const map<int, std::string>& m));
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
You still need a `typedef` if the return type contains an unprotected
Packit bd1cd8
comma, but that's much rarer.
Packit bd1cd8
Packit bd1cd8
Other advantages include:
Packit bd1cd8
  1. `MOCK_METHOD1(Foo, int, bool)` can leave a reader wonder whether the method returns `int` or `bool`, while there won't be such confusion using Google Mock's syntax.
Packit bd1cd8
  1. The way Google Mock describes a function type is nothing new, although many people may not be familiar with it.  The same syntax was used in C, and the `function` library in `tr1` uses this syntax extensively.  Since `tr1` will become a part of the new version of STL, we feel very comfortable to be consistent with it.
Packit bd1cd8
  1. The function type syntax is also used in other parts of Google Mock's API (e.g. the action interface) in order to make the implementation tractable. A user needs to learn it anyway in order to utilize Google Mock's more advanced features.  We'd as well stick to the same syntax in `MOCK_METHOD*`!
Packit bd1cd8
Packit bd1cd8
## My code calls a static/global function.  Can I mock it? ##
Packit bd1cd8
Packit bd1cd8
You can, but you need to make some changes.
Packit bd1cd8
Packit bd1cd8
In general, if you find yourself needing to mock a static function,
Packit bd1cd8
it's a sign that your modules are too tightly coupled (and less
Packit bd1cd8
flexible, less reusable, less testable, etc).  You are probably better
Packit bd1cd8
off defining a small interface and call the function through that
Packit bd1cd8
interface, which then can be easily mocked.  It's a bit of work
Packit bd1cd8
initially, but usually pays for itself quickly.
Packit bd1cd8
Packit bd1cd8
This Google Testing Blog
Packit bd1cd8
[post](http://googletesting.blogspot.com/2008/06/defeat-static-cling.html)
Packit bd1cd8
says it excellently.  Check it out.
Packit bd1cd8
Packit bd1cd8
## My mock object needs to do complex stuff.  It's a lot of pain to specify the actions.  Google Mock sucks! ##
Packit bd1cd8
Packit bd1cd8
I know it's not a question, but you get an answer for free any way. :-)
Packit bd1cd8
Packit bd1cd8
With Google Mock, you can create mocks in C++ easily.  And people might be
Packit bd1cd8
tempted to use them everywhere. Sometimes they work great, and
Packit bd1cd8
sometimes you may find them, well, a pain to use. So, what's wrong in
Packit bd1cd8
the latter case?
Packit bd1cd8
Packit bd1cd8
When you write a test without using mocks, you exercise the code and
Packit bd1cd8
assert that it returns the correct value or that the system is in an
Packit bd1cd8
expected state.  This is sometimes called "state-based testing".
Packit bd1cd8
Packit bd1cd8
Mocks are great for what some call "interaction-based" testing:
Packit bd1cd8
instead of checking the system state at the very end, mock objects
Packit bd1cd8
verify that they are invoked the right way and report an error as soon
Packit bd1cd8
as it arises, giving you a handle on the precise context in which the
Packit bd1cd8
error was triggered.  This is often more effective and economical to
Packit bd1cd8
do than state-based testing.
Packit bd1cd8
Packit bd1cd8
If you are doing state-based testing and using a test double just to
Packit bd1cd8
simulate the real object, you are probably better off using a fake.
Packit bd1cd8
Using a mock in this case causes pain, as it's not a strong point for
Packit bd1cd8
mocks to perform complex actions.  If you experience this and think
Packit bd1cd8
that mocks suck, you are just not using the right tool for your
Packit bd1cd8
problem. Or, you might be trying to solve the wrong problem. :-)
Packit bd1cd8
Packit bd1cd8
## I got a warning "Uninteresting function call encountered - default action taken.."  Should I panic? ##
Packit bd1cd8
Packit bd1cd8
By all means, NO!  It's just an FYI.
Packit bd1cd8
Packit bd1cd8
What it means is that you have a mock function, you haven't set any
Packit bd1cd8
expectations on it (by Google Mock's rule this means that you are not
Packit bd1cd8
interested in calls to this function and therefore it can be called
Packit bd1cd8
any number of times), and it is called.  That's OK - you didn't say
Packit bd1cd8
it's not OK to call the function!
Packit bd1cd8
Packit bd1cd8
What if you actually meant to disallow this function to be called, but
Packit bd1cd8
forgot to write `EXPECT_CALL(foo, Bar()).Times(0)`?  While
Packit bd1cd8
one can argue that it's the user's fault, Google Mock tries to be nice and
Packit bd1cd8
prints you a note.
Packit bd1cd8
Packit bd1cd8
So, when you see the message and believe that there shouldn't be any
Packit bd1cd8
uninteresting calls, you should investigate what's going on.  To make
Packit bd1cd8
your life easier, Google Mock prints the function name and arguments
Packit bd1cd8
when an uninteresting call is encountered.
Packit bd1cd8
Packit bd1cd8
## I want to define a custom action.  Should I use Invoke() or implement the action interface? ##
Packit bd1cd8
Packit bd1cd8
Either way is fine - you want to choose the one that's more convenient
Packit bd1cd8
for your circumstance.
Packit bd1cd8
Packit bd1cd8
Usually, if your action is for a particular function type, defining it
Packit bd1cd8
using `Invoke()` should be easier; if your action can be used in
Packit bd1cd8
functions of different types (e.g. if you are defining
Packit bd1cd8
`Return(value)`), `MakePolymorphicAction()` is
Packit bd1cd8
easiest.  Sometimes you want precise control on what types of
Packit bd1cd8
functions the action can be used in, and implementing
Packit bd1cd8
`ActionInterface` is the way to go here. See the implementation of
Packit bd1cd8
`Return()` in `include/gmock/gmock-actions.h` for an example.
Packit bd1cd8
Packit bd1cd8
## I'm using the set-argument-pointee action, and the compiler complains about "conflicting return type specified".  What does it mean? ##
Packit bd1cd8
Packit bd1cd8
You got this error as Google Mock has no idea what value it should return
Packit bd1cd8
when the mock method is called.  `SetArgPointee()` says what the
Packit bd1cd8
side effect is, but doesn't say what the return value should be.  You
Packit bd1cd8
need `DoAll()` to chain a `SetArgPointee()` with a `Return()`.
Packit bd1cd8
Packit bd1cd8
See this [recipe](CookBook.md#mocking_side_effects) for more details and an example.
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
## My question is not in your FAQ! ##
Packit bd1cd8
Packit bd1cd8
If you cannot find the answer to your question in this FAQ, there are
Packit bd1cd8
some other resources you can use:
Packit bd1cd8
Packit bd1cd8
  1. read other [documentation](Documentation.md),
Packit bd1cd8
  1. search the mailing list [archive](http://groups.google.com/group/googlemock/topics),
Packit bd1cd8
  1. ask it on [googlemock@googlegroups.com](mailto:googlemock@googlegroups.com) and someone will answer it (to prevent spam, we require you to join the [discussion group](http://groups.google.com/group/googlemock) before you can post.).
Packit bd1cd8
Packit bd1cd8
Please note that creating an issue in the
Packit bd1cd8
[issue tracker](https://github.com/google/googletest/issues) is _not_
Packit bd1cd8
a good way to get your answer, as it is monitored infrequently by a
Packit bd1cd8
very small number of people.
Packit bd1cd8
Packit bd1cd8
When asking a question, it's helpful to provide as much of the
Packit bd1cd8
following information as possible (people cannot help you if there's
Packit bd1cd8
not enough information in your question):
Packit bd1cd8
Packit bd1cd8
  * the version (or the revision number if you check out from SVN directly) of Google Mock you use (Google Mock is under active development, so it's possible that your problem has been solved in a later version),
Packit bd1cd8
  * your operating system,
Packit bd1cd8
  * the name and version of your compiler,
Packit bd1cd8
  * the complete command line flags you give to your compiler,
Packit bd1cd8
  * the complete compiler error messages (if the question is about compilation),
Packit bd1cd8
  * the _actual_ code (ideally, a minimal but complete program) that has the problem you encounter.