How can I mock object which is created with the action in the runtime? In the following code I want to mock the Connection class for the unit test.
private readonly Dictionary<IPEndPoint, IDeviceConnection> _connections = new Dictionary<IPEndPoint, IDeviceConnection>();
private async Task DiscoveryTask(CancellationToken stoppingToken)
{
try
{
while (!stoppingToken.IsCancellationRequested)
{
// Action which allows new Connection to be created and added to dict
_connections.Add(
ipEndPoint,
new DeviceConnection(
ipEndPoint,
new TcpClient(),
stoppingToken));
_connections[ipEndPoint].StartConnectionTask();
}
}
}
catch (Exception ex)
{
}
}
Advices on modification the code is acceptable.
1