GMock is not reporting failure to call expected methods

765 views
Skip to first unread message

Alain Désilets

unread,
Dec 19, 2013, 5:33:28 PM12/19/13
to googl...@googlegroups.com
I'm an experienced TDD, but a complete newbie w.r.t. GMock.

I am trying to write a mock for a class EmailSender.

I created the following test file:

== GMockSpikeTests.cpp ==
#include "stdafx.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "EmailSenderMock.h"

using ::testing::_;

class GMockSpikeTests : public testing::Test {
protected:

};

TEST_F(GMockSpikeTests, ThisShouldFailBecauseWeNeverInvokeTheMethod)
{
    EmailSenderMock *sender = new EmailSenderMock();
    EXPECT_CALL(*sender, sendMail(_, "title", _));
}
============

When I run the tests, I would expect the test to fail, because I am never actually calling the sendMail() method, for which I was expecting a call with "title" as the second argument.

Yet, the test succees, and I get this output when I run the tests:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from GMockSpikeTests
[ RUN      ] GMockSpikeTests.ThisShouldFailBecauseWeNeverInvokeTheMethod
[       OK ] GMockSpikeTests.ThisShouldFailBecauseWeNeverInvokeTheMethod (0 ms)
[----------] 1 test from GMockSpikeTests (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (5 ms total)
[  PASSED  ] 1 test.

Am I missing something?

BTW: my test runner looks like this:

== MainTestRunner.cpp ==
#include "stdafx.h"
#include <iostream>
#include <string>
#include "gtest/gtest.h"
#include "gmock/gmock.h"

using namespace std;

GTEST_API_ int main(int argc, char **argv) {
  printf("Running main() from gtest_main.cc\n");

  // testing::InitGoogleTest(&argc, argv);
  ::testing::InitGoogleMock(&argc, argv);
  int status = RUN_ALL_TESTS();

  string mystr;
  cout << "\nPress any key to exit...";
  getline (cin, mystr);

  return status;
}
===

And the EmailSenderMock.h file looks like this:

== EmailSenderMock.h ==
#pragma once
#include "EmailSender.h"
#include "gmock/gmock.h"
#include <list>
#include <string>

using namespace std;

class EmailSenderMock :
    public EmailSender
{
public:
    EmailSenderMock();
    ~EmailSenderMock();

    MOCK_METHOD3(sendMail, void(list<string> to, string title, string content));
};
===

Billy Donahue

unread,
Dec 19, 2013, 5:49:27 PM12/19/13
to Alain Désilets, googl...@googlegroups.com

Guess:

You're leaking the sender.

It never gets destroyed, so it never knows when to stop waiting for that method call.


--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/41c1f705-36e9-4149-9d3d-4a3b0ed61ecc%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages