ユーザーから提供されたユーザー/パスワードを使用して FTP 接続をテストしています。
単体テストを個別に実行すると、すべて正常に動作します。しかし、 のFTP_Test_Sucessful
前にを実行して順序付きテストを実行するFTP_Test_Unsuccessful
と、FTP_Test_Unsuccessful
テストは失敗します。間違ったパスワードを指定しても、FTP 接続は成功します。接続が成功した後です。
Passed Unit Test FTP_Test_Unsuccessful 00:00:02.3566157
Passed Unit Test FTP_Test_Sucessful 00:00:00.3048244
Failed Unit Test FTP_Test_Unsuccessful 00:00:00.2696941
Not Executed Unit Test FTP_Test_Sucessful 00:00:00
FTP接続を「リセット」するにはどうすればよいので、FTP_Test_Unsuccessful
実際に接続を正しくテストします。
これが私の単体テストです:
'''<summary>
'''basic Unsuccessful FTP test
'''</summary>
<TestMethod()>
Public Sub FTP_Test_Unsuccessful()
_TestSettings.FTPEnabled = True
_TestSettings.ChangeFTPCredentials("User", "BadPasword")
Try
Assert.IsFalse(_TestSettings.TestFtpSettings())
Catch ex As AssertFailedException
Throw
Catch ex As Net.WebException
Dim FTPResponse As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
Assert.AreEqual(FtpStatusCode.NotLoggedIn, FTPResponse.StatusCode)
Catch ex As Exception
Assert.Fail()
End Try
End Sub
'''<summary>
'''basic Sucessful FTP test
'''</summary>
<TestMethod()>
Public Sub FTP_Test_Sucessful()
_TestSettings.FTPEnabled = True
_TestSettings.ChangeFTPCredentials("User", "GoodPasword")
Assert.IsTrue(_TestSettings.TestFtpSettings())
End Sub
そして、ここに関連するコードがあります:
Public Function TestFtpSettings() As Boolean
Dim FTPConnectionTestedSuccessfully As Boolean = False
Dim response As FtpWebResponse = GetTestFtpResponse()
Dim StatusCode As System.Net.FtpStatusCode = response.StatusCode
response.Close()
FTPConnectionTestedSuccessfully = (StatusCode = Net.FtpStatusCode.OpeningData)
Return FTPConnectionTestedSuccessfully
End Function
Private Function GetTestFtpResponse() As FtpWebResponse
Dim request As FtpWebRequest = GetFTPConnection(WebRequestMethods.Ftp.ListDirectory)
Dim response As FtpWebResponse = GetFTPResponse(request)
Return (response)
End Function
Private Function GetFTPConnection(Method As String) As FtpWebRequest
'Get the object used to communicate with the server.
Dim FTPAdress As Uri = New Uri(String.Format("ftp://{0}:{1}", _
Me.FTPServerName, _
Me.FTPPort))
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(FTPAdress), _
FtpWebRequest)
request.Method = Method
request.Credentials = New NetworkCredential(Me.FTPUserName,
Me.GetDecryptedPassword)
Return request
End Function
Public Function GetFTPResponse(request As FtpWebRequest) As FtpWebResponse
Return DirectCast(request.GetResponse(), FtpWebResponse)
End Function