我如何moq这个DotNetOpenAuth Response对象?
-
03-07-2019 - |
题
我有一种方法可以接收 DotNetOpenAuth (正式名称为DotNetOpenId)<代码>响应对象。我的方法提取任何声称的数据,检查我的系统中是否存在该用户,yadda yadda yadda ...并且当完成时返回auth'd用户实例。
现在..我如何使用 moq 来模拟这个响应对象,来测试我的身份验证方法( AuthenticateUser()
)?
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
User user = null;
try
{
// Extract the claimed information and
// check if this user is valid, etc.
// Any errors with be thrown as Authentication Errors.
user = _authenticationService.AuthenticateUser(response) as User;
}
catch (AuthenticationException exception)
{
ViewData.ModelState.AddModelError("AuthenticationError", exception);
}
.. other code, like forms auth, other response.status' etc. ..
}
模拟框架:moq
语言:.NET C#3.5 sp1
响应对象:取自DotNetOpenAuth框架
解决方案
我对Moq并不熟悉,但是响应对象是一个实现 DotNetOpenAuth.OpenId.RelyingParty.IAuthenticationResponse
的类型,所以可以通过创建一个实现相同的界面,并准备返回相同的值。
...刚刚下载了Moq并模拟了IAuthenticationResponse,如下所示:
var response = new Mock<IAuthenticationResponse>(MockBehavior.Loose);
response.SetupGet(r => r.ClaimedIdentifier)
.Returns("http://blog.nerdbank.net/");
response.SetupGet(r => r.Status)
.Returns(AuthenticationStatus.Authenticated);
response.SetupGet(r => r.FriendlyIdentifierForDisplay)
.Returns("blog.nerdbank.net");
IAuthenticationResponse resp = response.Object;
Console.WriteLine(resp.ClaimedIdentifier);
显然,不是将结果发送到 Console.WriteLine
,而是希望将 resp
对象传递给您正在测试的方法。
不隶属于 StackOverflow