Skip to content

Add interface for SshCommand #1507

Open

Description

@EugenVeracity

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

Activity

  1. k0ka commented on Jul 31, 2025

    @k0ka

    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
                    )
                );
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions