English 中文(简体)
如何在高温克中重新确定习惯
原标题:how to set custom ref-variable in gmock
  • 时间:2012-01-13 03:59:41
  •  标签:
  • googlemock

I am using gmock in my project and I meet a problem to set a custom reference variable for a mock function. Suppose I have a class as following:

class XXXClient {
public:
    void QueryXXX(const Request&, Response&);
}; 

class XXXRunner {
public:
    void DoSomething(XXXClient&);
};

客户等级为XXXRunner: DoSomething using XXXClient:QueryXXX, and I Want to mock XXXClient to test XXXRunner:DoSomething.

问题是,KeryXXX的第二个参数,即答复,不是回报价值,而是参考变量,我把一些数据填入第三十次的答复:QueryXXX。 我想为答复建立一个习俗数据,以核实30Runner的不同状况:DoSomething。

振克框架可以确定预期的回归价值,但我无法找到一种办法确定“回归变量”?

So How to do so?

最佳回答

首先,打上<代码>XXXClient mock category>,请将其名称改为XXXClientMock:

class XXXClientMock : public XXXClient
{
public:
    MOCK_METHOD2(QueryXXX, QueryResult (Request&, Response&));
};

然后,使用GMock 行动SetArgReferee确定习惯参数如下:

TEST(XXXRunnerTC, SetArgRefereeDemo)
{
    XXXCLientMock oMock;

    // set the custom response object
    Response oRsp;
    oRsp.attr1 = “…”;
    oRsp.attr2 = “any thing you like”;

    // associate the oRsp with mock object QueryXXX function
    EXPECT_CALL(oMock,  QueryXXX(_, _)).
        WillOnce(SetArgReferee<1>(oRsp));
    // OK all done

    // call QueryXXX
    XXXRunner oRunner;
    QueryResult oRst = oRunner.DoSomething(oMock);
    …

    // use assertions to verity your expectation
    EXPECT_EQ(“abcdefg”, oRst.attr1);
    ……
}

Summary
GMock provide a series of actions to make it convenient to mock functions, such as SetArgReferee for reference or value, SetArgPointee for pointer, Return for return, Invoke for invoke custom mock function (with simple test logic), you can see here for more details.

Enjoy it :) Thank you

问题回答

http://code.google.com/p/googlemock/wiki/CheatSheet#Actions”rel=“nofollow”>。





相关问题
Using gmock to mock objects copied in client code

I could really use some help mocking the dependencies in this scenario. I have a client function that I need to test. It takes a 3rd party API resource and calls a method on it, populating the rest ...

Has anyone compiled GMock for iOS

I am trying to compile GMock from sources for iOS. I want to compile it as an archive so that multiple iOS projects can use it. I am using cmake to build this module (the existing build system uses ...

How to mock a function with the signature `object ()`

I want to mock a method with the declaration A::B X(void). The definition is something as follows. class A { class B; virtual B X() = 0; }; class A::B { public: auto_ptr<int> ...

How to mock templated methods using Google Mock?

I am trying to mock a templated method. Here is the class containing the method to mock : class myClass { public: virtual ~myClass() {} template<typename T> void myMethod(T param);...

Is Google Mock a good mocking framework? [closed]

I am pioneering unit testing efforts at my company, and need need to choose a mocking framework to use. I have never used a mocking framework before. We have already chosen Google Test, so using ...

C++ Mock/Test boost::asio::io_stream - based Asynch Handler

I ve recently returned to C/C++ after years of C#. During those years I ve found the value of Mocking and Unit testing. Finding resources for Mocks and Units tests in C# is trivial. WRT Mocking, not ...

热门标签