2011-11-28 42 views
0

失敗時我在我的NUnit測試下面的代碼:起訂量VerifySet使用接口的屬性設置

[Test] 
    public void SmtpSend_UsingSystemCredentials_SetsSmtpClientCredentials() 
    { 
     const string smtppwd = "smtppwd"; 
     const string smtpuser = "smtpuser"; 
     const string smtphost = "smtphost"; 
     const int smtpport = 333; 

     _emailManager.Setup(x => x.ValidateSmtpSettings()).Returns(true); 
     _emailTransportConfig.SetupGet(x => x.SmtpPassword).Returns(smtppwd); 
     _emailTransportConfig.SetupGet(x => x.SmtpUsername).Returns(smtpuser); 
     _emailTransportConfig.SetupGet(x => x.SmtpHost).Returns(smtphost); 
     _emailTransportConfig.SetupGet(x => x.SmtpPort).Returns(smtpport);    
     _smtpClient.SetupAllProperties(); 

     _smtpHandler.Send(null, "[email protected]", "[email protected]", "subject", "body", true); 
     _smtpClient.VerifySet(x => x.Port = smtpport); 
     _smtpClient.VerifySet(x => x.Host = smtphost); 

     // Todo Why is this not working? 
     _smtpClient.VerifySet(x => x.Credentials = new NetworkCredential(smtpuser, smtppwd)); 
    } 

我可以看到代碼落入這樣的功能:

private void SetSystemSmtpAccountSettings() 
    {   
     _smtpClient.Credentials = new NetworkCredential 
     { 
      UserName = _emailTransportConfig.SmtpUsername, 
      Password = _emailTransportConfig.SmtpPassword 
     }; 
     _smtpClient.Host = _emailTransportConfig.SmtpHost; 
     _smtpClient.Port = _emailTransportConfig.SmtpPort; 
    } 

但我有什麼不明白爲什麼VerifySet調用失敗。

回答

1

正如@Daz劉易斯說,你VerifySet是針對不同的情況下檢查。相反,嘗試使用It.Is火柴表達式:

_smtpClient.VerifySet(x => x.Credentials = It.Is<NetworkCredential>(c => c.UserName == smtpuser && c.Password == smtppwd)); 
+0

這工作完美,謝謝! – jaffa

2

你是代碼示例可以使用一些澄清,但我相信你的問題在於試圖對new NetworkCredential實例進行驗證,該實例不是在測試方法中設置的實際實例,因此驗證失敗。看起來您正在嘗試驗證用戶名和密碼是否在Credentials屬性上正確設置。這是一個基於狀態的測試中更容易與一對夫婦斷言的驗證:

Assert.IsNotNull(_smtpClient.Credentials); 
Assert.AreEqual(_smtpClient.Credentials.UserName, smtpuser); 
Assert.AreEqual(_smtpClient.Credentials.Password, smtppwd); 
+0

嗨,我無法測試上述,因爲_smtpClient是一個Mocked對象。我認爲你無法對NetworkCredential實例進行測試是正確的。我如何解決特定問題?我正在測試客戶端對象是從一個模擬對象獲取屬性,並針對另一個模擬對象進行設置。它只是NetworkCredential行不起作用。其他SetupGet/VerifySet對正常工作。 – jaffa

+0

查看@ adrift的回答 - Daz是正確的,那就可以解決它。 – TrueWill

0

我有類似的情況,但使用的工廠,所以擁有憑據類是一個模擬。

mockCreate = new Mock<IWebRequestCreate>(); 
mockRequest = new Mock<WebRequest>(); 
mockCreate.Setup(x => x.Create(It.IsAny<Uri>())) 
       .Returns(mockRequest.Object); 
webrequest = new HTTPRequestFactory(mockCreate.Object); 
mockRequest.SetupProperty(mr => mr.Credentials); 
mockRequest.SetupProperty(mr => mr.Credentials.UserName); 
mockRequest.SetupProperty(mr => mr.Credentials.Password); 

//setup username and password and send to factory 
WebResponse response = webrequest.GetWebResponse(testURI); 
mockCreate.VerifyAll(); 
mockRequest.VerifySet(mr => mr.Credentials = It.Is<NetworkCredential>(c => c.UserName == "user" && c.Password == "password")); 
Assert.AreEqual("user", mockRequest.Object.Credentials.UserName); 
Assert.AreEqual("user", mockRequest.Object.Credentials.Password); 

adrifts answer works,Daz's will not compile。有關何處尋找的任何提示可以更多地瞭解原因?智能感知說這是因爲ICredentials沒有定義UserNamePassword