SSH.NET
SSH.NET copied to clipboard
Add interface for SshCommand
Similar to #1486, it would be also be great to add an interface to SshCommand for testing, as this would allow mocking up the return value of SshClient.RunCommand().
This issue makes using NSubstitute (and any other mocker) to mock SSH.NET very hard. In order to mock main methods like RunCommand or CreateCommand, one must create SshCommand. But it is exact class with internal constructor which in turn uses internal class.
I made this workaround to substitute it, but it's quite clumsy:
var iSession = typeof(SshCommand).Assembly.GetType("Renci.SshNet.ISession");
if (iSession == null)
{
throw new Exception("Could not find the ISession interface");
}
var substitute = NSubstitute.Substitute.For<ISshClient>();
substitute.RunCommand(Arg.Any<string>())
.ReturnsForAnyArgs(info =>
NSubstitute.Substitute.For<SshCommand>(
NSubstitute.Substitute.For([iSession], []),
info.Arg<string>(),
Encoding.UTF8
)
);