C++(14) - googletest未定义标识符

C++ (14) - googletest undefined identifier

我使用的是 C++ 14。为什么 googletest 无法获取 curl_client class 对象指针? 我是否正确初始化了它在 CurlClientTest 中?

测试代码:

#include "../src/include/CurlClient.h"
#include <gtest/gtest.h>
#include <string>

class CurlClientTest : testing::Test {
public:
  SimpleCURLClient::CurlClient *curl_client;
  virtual void SetUp() {
    curl_client = new SimpleCURLClient::CurlClient(test_url);
  }

  virtual void TearDown() {
    delete curl_client;
  }

private:
  std::string test_url = "http://postman-echo.com/get?foo1=bar1&foo2=bar2";
};

TEST(CurlClientTest, CurlClientInitTest) {
  std::cout << curl_client->getCurlUrl << "\n";
}

代码 CurlClient.h:

#include <curl/curl.h>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#ifndef UTILS_CURLCLIENT_H
#define UTILS_CURLCLIENT_H

namespace SimpleCURLClient {
class CurlClient {
public:
  CurlClient(std::string remote_url, int ip_protocol = 1, int timeout = 10,
             bool follow_redirects = 1);
  ~CurlClient();
  void setCurlUrl(std::string &new_url);
  std::string getCurlUrl();
  void setOption(CURLoption curl_option_command, long curl_option_value);
  void setOption(CURLoption curl_option_command, std::string curl_option_value);
  void setHeader(std::vector<std::string> &header_list);
  std::pair<CURLcode, std::string> makeRequest();
  std::pair<CURLcode, std::string> makeRequest(std::string &post_params);
  std::pair<CURLcode, std::string> sendGETRequest();
  std::pair<CURLcode, std::string> sendPOSTRequest(std::string &post_params);
  std::pair<CURLcode, std::string> sendDELETERequest(std::string &post_params);

private:
  std::string m_curl_url;
  CURL *m_curl;
  struct curl_slist *m_curl_header_list;
};
} // namespace SimpleCURLClient

#endif // UTILS_CURLCLIENT_H

错误:

Build FAILED.

"E:\somepath\simple_curl_cpp\build\test\simple_curl_cpp_test.vcxproj" (default target) (1) ->
(ClCompile target) ->
  E:\somepath\simple_curl_cpp\test\CurlClientTest.cc(21): error C2065: 'curl_client': undeclared identifier [E:\somepath\simple_curl_cpp\build\test\simple_curl_cpp_test.vcxproj]
  E:\somepath\simple_curl_cpp\test\CurlClientTest.cc(21): error C2227: left of '->getCurlUrl' must point to class/struct/union/generic type [E:\somepath\simple_curl_cpp\build\test\simple_curl_cpp_test.vcxproj]

答案(Chris Olsen 在评论中给出):

我们必须使用 TEST_F 而不是 TEST。同时将 CurlClientTest 更改为 public。以下测试代码有效。

#include "../src/include/CurlClient.h"
#include <gtest/gtest.h>
#include <string>

class CurlClientTest : public testing::Test {
public:
  SimpleCURLClient::CurlClient *curl_client;
  virtual void SetUp() {
    curl_client = new SimpleCURLClient::CurlClient(test_url);
  }

  virtual void TearDown() {
    delete curl_client;
  }

private:
  std::string test_url = "http://postman-echo.com/get?foo1=bar1&foo2=bar2";
};

TEST_F(CurlClientTest, CurlClientInitTest) {
  std::cout << curl_client->getCurlUrl() << "\n";
}

使用固定装置的测试需要使用 TEST_F 宏。有关详细信息,请参阅 Google 测试入门中的 Test Fixtures

TEST_F(CurlClientTest, CurlClientInitTest) {
  std::cout << curl_client->getCurlUrl << "\n";
}