小编Rag*_*gav的帖子

如何在 C++ 中的普通或单例类中谷歌测试私有方法/枚举类

我需要对单例类的私有方法进行 Gtest。我尝试使用单身人士的 Friend 类。但没有帮助。它说私人不能被调用。我添加了类,Gtest 尝试,输出错误。随时询问更多澄清,提前致谢!!如果有错别字请忽略。。

下面要测试的单例类:

class Listener: public CommonListen {
 public:
  friend class test_listener;
  Listener(task_id taskid, const string thread_name) :
    CommonListen(taskid, thread_name ) { }
  static Listener* GetInstance() {
     if (Listener_ptr_ == nullptr) {
       Listener_ptr_ = new
           Listener(LISTENER_ID, ListenerName);
     }
     return Listener_ptr_;
  }

 private:
  // Override the base's method
  void SetupPub();
  static Listener* Listener_ptr_;
};
Run Code Online (Sandbox Code Playgroud)

我使用 Friend 类厌倦了 GTest,如下所示:

class test_listener : public ::testing::Test {
 public:
  void call_private(void);
};

TEST_F(test_listener, create_listener) {
  call_private();
}
void test_listener::call_private() {
  (Listener::GetInstance())->SetupPub();
} …
Run Code Online (Sandbox Code Playgroud)

c++ mocking googletest gmock

5
推荐指数
0
解决办法
1443
查看次数

如何将std :: string复制到unsigned char数组?

如何将std :: string(样本)复制到unsigned char数组(陷阱)?

int main() {
    unsigned char trap[256];
    std::string sample = ".1.3.6.1.4";
    strcpy(trap,sample.c_str());
    std::cout << trap << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码抛出错误:

time.cpp: In function ‘int main()’:
time.cpp:20: error: invalid conversion from ‘unsigned char*’ to ‘char*’
time.cpp:20: error:   initializing argument 1 of ‘char* strcpy(char*, const char*)’
Run Code Online (Sandbox Code Playgroud)

c++ arrays string type-conversion

4
推荐指数
1
解决办法
7393
查看次数

标签 统计

c++ ×2

arrays ×1

gmock ×1

googletest ×1

mocking ×1

string ×1

type-conversion ×1