You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SftpClient.OpenAsync should return Stream for ease of mocking #1691
SftpClient.OpenAsync currently returns an SftpFileStream which cannot be mocked by NSubstitute or even faked because it has private/internal constructors.
It would be nice if OpenAsync was instead defined as returning Stream, even though it actually returns a SftpFileStream. The only non-Stream public member that I can see is the Timeout property. If it's absolutely vital that this property is exposed, maybe define an ISftpFileStream interface instead? Or the timeout could be passed to OpenAsync.
I also ran into this when trying to mock. @doxxx describes it perfectly. I would consider the option of adding ISftpFileStream because, IMO, it would be nicer to mock than returning Stream. But anything is better than the current state.
There's a lot of assumptions in SSH.NET that the type returned by OpenAsync (and all the other similar methods) extends Stream. Not to mention all of the existing user code that could assume that these methods return a type that extends Stream. It would be a significantly breaking change to use an interface type instead.
Changing the return type to Stream however is much easier. Still requires a bunch of changes here and there, mostly in test code, which specifies the expected return type exactly, but easily fixed. The only drawback to this option is that you lose access to the Timeout property. That said, none of the test code actually accesses this property...
Activity
waldemarsson commentedon Sep 30, 2025
Any update on this? Trying to write unit tests for my integration and am stuck because I can't mock
OpenAsyncwaldemarsson commentedon Oct 3, 2025
As a workaround I just created a proxy service that wraps ISftpClient.
fkubicek commentedon Oct 31, 2025
I also ran into this when trying to mock. @doxxx describes it perfectly. I would consider the option of adding
ISftpFileStreambecause, IMO, it would be nicer to mock than returningStream. But anything is better than the current state.doxxx commentedon Oct 31, 2025
I took a look at this. It is not simple.
There's a lot of assumptions in SSH.NET that the type returned by
OpenAsync(and all the other similar methods) extendsStream. Not to mention all of the existing user code that could assume that these methods return a type that extendsStream. It would be a significantly breaking change to use an interface type instead.Changing the return type to
Streamhowever is much easier. Still requires a bunch of changes here and there, mostly in test code, which specifies the expected return type exactly, but easily fixed. The only drawback to this option is that you lose access to theTimeoutproperty. That said, none of the test code actually accesses this property...doxxx commentedon Nov 6, 2025
As @Rob-Hague said in #1725, the wrapper solution by @waldemarsson is the best option.