我正在使用C ++ 14。测试代码:
#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";
}
我使用的是c ++14。为什么googletest无法提取curl_client类对象的指针?我在CurlClientTest中正确初始化了吗?测试代码:#include“ ../src/include/CurlClient.h”#...
TEST_F
宏。有关更多信息,请参见Google Test Primer中的Test Fixtures。TEST_F(CurlClientTest, CurlClientInitTest) {
std::cout << curl_client->getCurlUrl << "\n";
}