我刚刚开始谷歌模拟并尝试编写第一个模拟测试,如下所示。 AWSS3io是一个rest API类,用于与AWS s3服务器通信以获取文件。我想测试-ve场景,即500响应代码。您能否提供有价值的评论,这些代码很好,或者还有其他方法可以让您更优雅。
================以下是代码。===================
File: AWSS3io.h
---------------------------start-----------------------------------
class AWSS3io {
private:
std::string m_aws_url;
public:
IO_STATUS_CODE get(const std::string &key, std::string &output_string, int &resp_code);
AWSS3io(const std::string & aws_url);
~AWSS3io();
};
#if GTEST
class MockAWSS3io
{
private:
std::string m_aws_url;
public:
MockAWSS3io(const std::string & aws_url) { m_aws_url = aws_url; }
~MockAWSS3io() {}
MOCK_METHOD3(get, IO_STATUS_CODE(const std::string &key, std::string &output_string, int &resp_code));
};
#endif
class AWSio
{
public:
AWSio(AWSS3io* paws);
~AWSio();
IO_STATUS_CODE get(const std::string &key, std::string &output_string);
#if GTEST
MockAWSS3io* aws_s3_io = nullptr;
#else
AWSS3io* aws_s3_io = nullptr;
#endif
};
---------------------------end-----------------------------------
file: tests/test_aws_mock_test.cpp
---------------------------start-----------------------------------
TEST(FileGetTest, FailureCase) {
const std::string t_aws_url = "aa.s3.awss3.com";
MockAWSS3io aws_s3_io(t_aws_url);
EXPECT_CALL(*aws_s3_io, get(_,_,_))
.Times(AtLeast(2)).WillOnce(testing::SetArgReferee<2>(500)).WillRepeatedly(testing::SetArgReferee<2>(200));
AWSio awsio(&aws_s3_io);
std::string key, value;
EXPECT_TRUE(awsio.get(key, value));
}
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
---------------------------end-----------------------------------