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

问题描述 投票:0回答:1

我正在使用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”#...
c++ c++14 googletest
1个回答
0
投票
使用夹具的测试需要使用TEST_F宏。有关更多信息,请参见Google Test Primer中的Test Fixtures

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

© www.soinside.com 2019 - 2024. All rights reserved.