Example of using gMock
In unit testing, particularly when interfacing with network operations, mocking is an invaluable technique. This is exemplified in the case of a Socket class, which serves as a foundational element for network communication. The Socket class abstracts the functionality of sending and receiving raw byte arrays over a network, providing methods such as send and recv. Concrete classes such as TcpSocket, UdpSocket, and WebSocket extend this base class to implement specific network protocols. The following code shows the definition of the Socket class:
class Socket {
public:
// sends raw byte array of given size, returns number of bytes sent
// or -1 in case of error
virtual ssize_t send(void* data, size_t size) = 0;
// receives raw byte array of given size, returns number of bytes received
// or -1 in case of error
virtual...