mercredi 28 janvier 2015

How to test method in google test, using std::function?

I would like to test method "methodToTest" in class A:


typedef std::function F_global;



struct A
{
F_global m_F_global;

A(F_global m_F_global) : p_F_global(m_F_global) {}

void methodToTest()
{
m_F_global(5);
}
};


I have got a mock class:


class RunMock { public: MOCK_METHOD1(run, void (int)); };


Below I have got a test case:



class TestClass : public testing::Test
{
protected:
void SetUp();

std::unique_ptr<A> m_aa;
StrictMockPtrMade<RunMock> m_runMock;
};

void UecSimplePortTestSuite::SetUp()
{
m_aa = make_unique<A>(m_runMock->AaSysComMsgForward);//IT DOESN'T WORK I DON'T KNOW HOW TO FORWARD A METHOD run from RunMock to constructor
}

TEST_F(UecSimplePortTestSuite, testForwardMessage)
{
EXPECT_CALL(*m_runMock, run(_));
m_aa->methodToTest();
}


Generally I don't know how transfer a method "run" from Mock class "RunMock" in "UecSimplePortTestSuite::SetUp()". I would like to do this, because I would like to "EXPECT_CALL(*m_runMock, run(_));" in UecSimplePortTestSuite.testForwardMessage to test "methodToTest()". I think that good idea could be to use lmbda, std::boost::bind or something like this.


Aucun commentaire:

Enregistrer un commentaire