我需要爲以下函數編寫一些單元測試。爲了做到這一點,我需要模擬ifstream對象。我這樣做了,但在測試過程中遇到了執行控制問題。我如何設置在 while(p_fileDescriptor->get(l_singleByte))
while(p_fileDescriptor->get(l_singleByte))
中返回的值,以便在一種情況下將eofbit對象返回到另一個case中的「normal」istream對象?我嘗試了一些選項,但它不起作用。你能幫我解決嗎? 波紋管功能有我的單元測試。 功能測試:Google Mock:模擬fstream對象和測試執行控制
void ServerSendFileRequestHandler::sendRequestedFile(std::shared_ptr<std::istream> p_fileDescriptor, int& p_clientSocket) const
{
unsigned int l_msgCounter = 1;
unsigned long long l_byteCounter = 0;
Message l_sendline;
memset(&l_sendline, 0, sizeof(l_sendline));
char l_singleByte;
while(p_fileDescriptor->get(l_singleByte))
{
checkIfReadByteSucceded(p_fileDescriptor, l_byteCounter);
l_sendline.payload[l_byteCounter] = l_singleByte;
l_byteCounter++;
if(l_byteCounter == PAYLOAD_SIZE)
{
std::cout << "Sending message number: " << l_msgCounter << std::endl;
sendClientSendFileInd(l_sendline, l_byteCounter, p_clientSocket);
l_msgCounter++;
l_byteCounter = 0;
memset(&l_sendline, 0, sizeof(l_sendline));
}
}
if(l_byteCounter)
{
std::cout << "Sending message number: " << l_msgCounter << std::endl;
sendClientSendFileInd(l_sendline, l_byteCounter, p_clientSocket);
l_msgCounter++;
}
std::cout << "Sending of file is done!" << std::endl;
}
我的單元測試:
#include "gmock/gmock.h"
#include <istream>
using namespace std;
class FstreamMock : public std::istream
{
public:
MOCK_METHOD1(get, istream&(char&));
};
using namespace testing;
TEST_F(ServerSendFileRequestHandlerTestSuite, sendRequestedFileTest)
{
int l_clientSocket = 0;
std::shared_ptr<FstreamMock> l_fileDescriptor;
const bool i = false;
char l_byte;
//EXPECT_CALL(*l_fileDescriptor, get(_)).WillRepeatedly(ReturnRef(*l_fileDescriptor));
EXPECT_CALL(*l_fileDescriptor, get(_)).WillRepeatedly(ReturnRef(*l_fileDescriptor);
m_sut.sendRequestedFile(l_fileDescriptor, l_clientSocket);
}