Steve Fox
unread,Nov 10, 2010, 6:42:31 PM11/10/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google C++ Mocking Framework
I have a test program that I did both with regular pointers and again
with boost::shared_ptr. The former runs just fine, but the latter
always segfaults. Finally, I redid the test with boost::shared_ptr but
no GMock and that worked fine.
I was using a system installed copy of gmock, but get the same results
when I use a manually built library (following the steps in the
README).
I only saw one other post in the archive referring to using
boost::shared_ptr, but I think I'm doing it right. I'm fairly
inexperienced with C++ so I hope I'm not doing something obviously
wrong.
Regular pointers with GMock
===========================================================
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class Bear {
public:
virtual ~Bear() {}
virtual void Roar() = 0;
};
class MockBear: public Bear {
public:
MOCK_METHOD0(Roar, void());
};
class Zoo {
public:
Zoo (Bear* bear) : mBear(bear) {}
void listen () { mBear->Roar(); }
private:
Bear* mBear;
};
TEST(BearTest, TestRoar) {
MockBear bear;
Bear* b = &bear;
EXPECT_CALL(bear, Roar());
Zoo z(b);
z.listen();
}
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
boost::shared_ptr with GMock
===========================================================
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
class Bear {
public:
virtual ~Bear() {}
virtual void Roar() = 0;
};
class MockBear: public Bear {
public:
MOCK_METHOD0(Roar, void());
};
class Zoo {
public:
Zoo (shared_ptr<Bear>& bear) : mBear(bear) {}
void listen () { mBear->Roar(); }
private:
shared_ptr<Bear> mBear;
};
TEST(BearTest, TestRoar) {
shared_ptr<MockBear> bear(new MockBear);
shared_ptr<Bear> b(b.get());
EXPECT_CALL(*bear, Roar());
Zoo z(b);
z.listen();
}
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
boost::shared_ptr without GMock
===========================================================
#include <gtest/gtest.h>
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
class Bear {
public:
void Roar() { std::cout << "Roar!\n"; }
};
class Zoo {
public:
Zoo (shared_ptr<Bear>& bear) : mBear(bear) {}
void listen () { mBear->Roar(); }
private:
shared_ptr<Bear> mBear;
};
TEST(BearTest, TestRoar) {
shared_ptr<Bear> bear(new Bear);
Zoo z(bear);
z.listen();
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}