I have a Visual Studio 2008 C++ project where I am implementing unit tests for a class that uses a boost::asio::ip::udp::socket. To get to unit testability, I need to be able to pass in a Mock of that socket object.
My problem is that you can’t have a virtual template function and boost::asio has a heavily templatized user interface.
How does one create an interface to mock for a C++ library that uses templates?
// socket interface that can be mocked
class IUDPSocket
{
public:
virtual void open( const boost::asio::ip::udp& protocol ) = 0;
// problem!
template <typename SettableSocketOption>
virtual void set_option( const SettableSocketOption& option ) = 0;
virtual void bind( const boost::asio::ip::udp::endpoint& endpoint ) = 0;
// Problem!
template <typename MutableBufferSequence, typename ReadHandler>
virtual void async_receive_from(
const MutableBufferSequence& buffers,
boost::asio::ip::udp::endpoint& sender_endpoint,
ReadHandler handler ) = 0;
protected:
virtual ~IUDPSocket() = 0;
};
// socket class that will be used for "normal" operation.
class BoostUDPSocket
{
// ...
virtual void open( const boost::asio::ip::udp& protocol )
{
socket_.open( protocol );
};
private:
boost::asio::ip::udp::socket socket_;
};
If it matters, I am using Google Test as my unit test framework.
Thanks
Two possibilities:
boost::asioin your interface directly, use abstraction if the types are complex enough:IMutableBufferSequence,IEndpointetc…