Search code examples
c++c++14googletest

C++ (14) - googletest undefined identifier


I'm using c++ 14. Why isn't googletest able to pick up the curl_client class object pointer? Did I initialize it correctly in CurlClientTest?

Testing code:

#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";
}

code for 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

Error:

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]

ANSWER (GIVEN BY Chris Olsen in comments) :

We have to use TEST_F and NOT TEST. Also change CurlClientTest to public. The below code for the test works.

#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";
}

Solution

  • Tests using fixtures require use of the TEST_F macro. See Test Fixtures in the Google Test Primer for more info.

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