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));
};
===