How would you unit test FTPWebRequest and FTPWebResponse through MOQ.
Unit testing FTPWebRequest/FTpWebResponse
1.5k Views Asked by Dev-Mo At
3
There are 3 best solutions below
1

You can't mock FTPWebRequest or FTPWebResponse with Moq, because it only allows you to mock interfaces or abstract classes. And it doesn't look like MS was thinking about testability when they wrote most of the System.Net namespace. That's the primary reason I've moved away from Moq to RhinoMocks.
You'll need to build your own FTPWeb* objects and pass them to your handler.
0

For that i use Rhino frameWork.
It can handle instance creation even if there is no public constructor, read only properties and more.
Example:
var ftpWebResponse = Rhino.Mocks.MockRepository.GenerateStub<FtpWebResponse>();
ftpWebResponse.Stub(f=>f.StatusCode).Return(FtpStatusCode.AccountNeeded);
Not possible with Mock also because
FTPWebResponse
doesn't have constructors exposed to allow something to be derived from it.Here is how I wrote my test in similar situation.
Method under test:
ExceptionContainsFileNotFound(Exception ex)
contains following logic:In order to test it I implemented quick trick.
(Of course FtpFileNotFoundStatus is not exposed to the World.)