dimanche 2 avril 2017

gmock EXPECT_CALL does not invoke the method in tested class

I am new to gmock testing. I have Foo class which has member functions set() and start(). Start() triggers set() and class members member1 set to 1 , member2 is set to 2. When i try to test this in gmock i could not see these members are set.Why and how can i resolve this? Thanks.

test.h file

#ifndef TEST_H_
#define TEST_H_


#include <gtest/gtest.h>
#include <gmock/gmock.h>


class Foo
{
public:
    Foo();
    virtual~Foo();
    int member1;
    int member2;
    virtual void set();
    virtual void start();
};

class MockFoo:public Foo
{
public:
    MOCK_METHOD0(set, void());

};

#endif /* TEST_H_ */

simpleGmock.cpp file

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

using ::testing::AtLeast;
using ::testing::Return;


Foo::Foo(){};
Foo::~Foo(){};
void Foo::set()
{
    member1=1;
    member2=2;
}
void Foo::start()
{
    this->set();
}

TEST(simplesuite, callmethods)
{
    MockFoo mockfoo_obj;

    EXPECT_CALL(mockfoo_obj, set())
          .Times(AtLeast(1))
          .WillOnce(::testing::Return());

    mockfoo_obj.start();

    ASSERT_EQ(mockfoo_obj.member1,1);


}
int main( int argc, char *argv[] ) {
    ::testing::InitGoogleMock( &argc, argv );
    return RUN_ALL_TESTS( );
}

and the result is below:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from simplesuite
[ RUN      ] simplesuite.callmethods
simpleGmock.cpp:39: Failure
      Expected: mockfoo_obj.member1
      Which is: -528070912
To be equal to: 1
[  FAILED  ] simplesuite.callmethods (0 ms)
[----------] 1 test from simplesuite (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] simplesuite.callmethods

 1 FAILED TEST

Aucun commentaire:

Enregistrer un commentaire