gmock

来源:互联网 发布:centos卸载lamp环境 编辑:程序博客网 时间:2024/05/22 11:09

gmock

分类: C/C++2012-01-01 10:18 1746人阅读 评论(0) 收藏 举报
testingdistanceclassapidelete

gtest其实是googlemock(简称gmock)的一个模块,gmock的下载包中包含gtest。gmock的主页为:http://code.google.com/p/googlemock/

三篇学习文章:

1,http://code.google.com/p/googlemock/wiki/ForDummies

2,http://code.google.com/p/googlemock/wiki/CheatSheet

3,http://code.google.com/p/googlemock/wiki/CookBook

按上述顺序阅读,最后一篇文章比较长。


示例1:基本功能

// ITurtle.h
#ifndef __I_TURTLE_H__
#define __I_TURTLE_H__


class ITurtle
{
public:
   >    virtual void PenDown() = 0;
   >    virtual void TurnRight(int degrees) = 0; // 顺时针旋转一定的角度
   >    virtual int GetX() const = 0;
   >};


#endif // __I_TURTLE_H__


// Painter.h
#ifndef __PAINTER_H__
#define __PAINTER_H__


#include "ITurtle.h"


class CPainter
{
private: 
    ITurtle*>} 


CPainter::~CPainter()
{


}


void CPainter::SetTurtle(ITurtle*>} 


void CPainter::DrawSquare(int>    {
       >    }


   >    }


   >    };
} //>    };
} //>        if (!poParamIF)
        {
           >        int bIsUseAlipay = 0;


        UVariantField*>

       >

       >

       >    CMockParamIF* poMockParamIF = new CMockParamIF;


    EXPECT_CALL(*poMockAPIProvider, GetParamIF())
        .Times(AtLeast(1))
        .WillRepeatedly(Return(poMockParamIF)); // 这里就是对返回值打桩,如果不指定,则返回默认的(返回值为整型的函数默认返回0,为布尔型的默认返回false,为指针的默认返回NULL)


    UVariantField>

    UVariantField>

    EXPECT_CALL(*poMockParamIF, GetParam(_, _))
        .Times(AtLeast(1))
        .WillOnce(DoAll(SetArgPointee<1>(unRetailWholesaleValue), Return(1))) // 这些语法参考学习文章或者下面的“设置参数示例”
        .WillRepeatedly(DoAll(SetArgPointee<1>(unDefaultValue), Return(1)));


    Rank>};

class XXXClientMock :>  EXPECT_CALL(mutator, Mutate(NotNull(), 5))
      .WillOnce(SetArrayArgument<0>(values,>

2,可以只mock感兴趣的接口,不感兴趣的会继承父类中的实现,即:

class IAbc
{
public:
   >

    virtual int Func2() { return 100; }
};

class CMockAbc : public IAbc
{
public:
    MOCK_METHOD0(Func1, int());
};

通过mock对象调用Func2时,会使用父类中的实现,返回100

0 0
原创粉丝点击