SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

Add interface for SshCommand

Open EugenVeracity opened this issue 1 year ago • 1 comments

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().

EugenVeracity avatar Sep 26 '24 16:09 EugenVeracity

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
                )
            );

k0ka avatar Jul 31 '25 10:07 k0ka