2009-06-09 32 views
1

我有一個AccountController其構造函數需要從我的自定義IOpenIdAuthentication接口派生的對象。默認情況下,這是一個包含OpenIdRelyingParty的對象。接口看起來是這樣的:如何對使用DotNetOpenId的ASP.NET MVC控制器進行單元測試?

public interface IOpenIdAuthentication { 
    IAuthenticationResponse Response { get; } 
    IAuthenticationRequest CreateRequest(string identifier); 
} 

我可以嘲笑IAuthenticationResponse

_mockResponse = new Mock<IAuthenticationResponse>(MockBehavior.Loose); 
_mockResponse.SetupGet(r => r.ClaimedIdentifier).Returns(identifier); 
_mockResponse.SetupGet(r => r.Status) 
    .Returns(AuthenticationStatus.Authenticated); 
// ... removed the formatting of 'friendlyId' ... 
_mockResponse.SetupGet(r => r.FriendlyIdentifierForDisplay).Returns(friendlyId); 

不過,我不知道如何嘲笑IAuthenticationRequest,因爲它似乎要複雜得多。有任何想法嗎?

回答

1

這並不複雜。如果您只進行身份驗證,那麼嘲諷RedirectToProvider()就足夠了。在最簡單的情況下,它看起來像:

_mockRequest = new Mock<IAuthenticationRequest>(MockBehavior.Strict); 
_mockRequest.Setup(r => r.RedirectToProvider()); 

希望這有助於

+0

啊,謝謝!我之前沒有使用模擬框架,所以我不確定是否必須模擬對象的每個部分。 – 2009-06-09 21:05:53

相關問題