gmock: NiceMock and MockFunction

Viewed 130

I was wondering whether it is possible to use NiceMock together with MockFunction.

What I currently have is more-the-less the following:

struct ApiT {
  std::function<int(void*)> bar;
  std::function<int(void*)> baz;
  // ...
};

struct MockT {
  ApiT api;
  MockFunction<int(void*)> bar;
  MockFunction<int(void*)> baz;
  MockT() {
    api.bar = bar.asStdFunction();
    api.baz = baz.asStdFunction();
  }
};

TEST(foo_test, test_bar_is_being_called_by_foo) {
  MockT mock;
  EXPECT_CALL(mock.bar, Call(_)).WillOnce(Return(0));
  foo(mock.api); // foo calls bar and baz
}

Problems/observations:

  • The test basically works, but I see an ugly "uninteresting mock function call ..." because there is no expectation on baz
  • I tried with NiceMock<MockT> mock; instead of MockT mock;, but that doesn't seem to work, i.e. I still see the warning
  • I don't want to state any expectations on baz being called as I don't care about it in this test
  • I do need and want to mock baz as its original execution takes long and requires some setup
  • I think I could change ApiT to use (virtual) functions instead of std::function and then use MOCK_METHOD and NiceMock. However, as the code already exists, I was wondering whether there exists a way to use NiceMock together with MockFunction.
0 Answers
Related