简体   繁体   English

我想为下面的场景编写gmock google测试用例

[英]i want to write gmock google test cases for the below scenario

i have set of functions within the Singleton class. 我在Singleton类中有一组函数。 i want to mock a function in the singleton class. 我想在单例类中模拟一个函数。 Lets take the below piece of code.The function setname() will return the string from the classyyy's setname() funciton. 让我们采取下面的代码。函数setname()将返回classyyy的setname()函数中的字符串。 so here i want to test the return value.so please tell me how to write the test case for this situation. 所以在这里我想测试返回值。请告诉我如何为这种情况编写测试用例。

class mockBtMxxx : public BTMxxx
{
public:
    MOCK_METHOD2(setname, string(const int& id, const string& name));
};

// Test case for Setting Local Device Friendly Name.
TEST(TestBTC, GMockSetNameTest)
{
    mockBtMxxx mock_Btm;
    int id = 12345;
    string str = "Hello";
    EXPECT_CALL(mock_Btm, setname(_,_)).WillOnce(Return("Hello"));
}

I am getting the below errors : error: 'BTMxxx::BTMxxx()' is private gmock-actions.h:491:66: error: no matching function for call to 'ImplicitCast_(const char*&)' 我收到以下错误:错误:'BTMxxx :: BTMxxx()'是私有gmock-actions.h:491:66:错误:没有匹配函数调用'ImplicitCast_(const char *&)'

For your first error, you need to make the constructor of your base class callable from the derived class. 对于第一个错误,您需要使派生类可调用基类的构造函数。 While you have not shown the declaration of class BTM , it is easy to guess that you have the constructor declared as private currently. 虽然您没有显示class BTM的声明,但很容易猜到您当前将构造函数声明为private I suggest making BTMxxx::BTMxxx() protected so it can be called by the mock class's default constructor. 我建议protected BTMxxx::BTMxxx()以便它可以被mock类的默认构造函数调用。

As to your second error, the return type of setname is probably being deduced as const char* . 至于你的第二个错误, setname的返回类型可能被推断为const char* You need to provide a std::string object in order for it to match the exact return type. 您需要提供一个std::string对象,以使其与确切的返回类型匹配。

For a more complete picture of your problem, please provide the actual code for the BTMxxx class and specify what platform and compiler you are using. 要更全面地了解您的问题,请提供BTMxxx类的实际代码,并指定您正在使用的平台和编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM