c# - 单元测试 dotnetopenauth ctp

标签 c# asp.net-mvc dotnetopenauth

我正在使用 DotNetOpenAuth CTP 库实现一个 oauth 提供程序。所以我创建了一个 mvc3 应用程序,它有一个 OAuth Controller ,其中有 3 个方法,目的是授权第三方应用程序。 Controller 有一个 IOAuthService,它封装了库为完成某些任务必须执行的所有逻辑,但是,服务方法返回 DotNetOpenOAuth 对象,其构造函数受到保护。

我想测试我的 OAuthController 中方法的行为,为此,我试图模拟我的服务方法,但我无法做到这一点。我必须告诉 moq 库我期望服务方法返回什么类型的对象,并且由于我无法访问这些对象的构造函数,所以我无法对我的 Controller 方法执行测试。

Controller :

public class OAuthController : Controller
{
    private readonly IOAuthService _oAuthService;

    public OAuthController(IOAuthService oAuthService)
    {
        _oAuthService = oAuthService;
    }

    [Authorize, AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult Authorize()
    {
        ClientApplication requestingClient;
        var request = _oAuthService.ReadAuthorizationRequest();
        if (request == null)
        {
            throw new HttpException((int)HttpStatusCode.BadRequest, "Missing authorization request.");
        }

        var response = _oAuthService.RequestClientAuthorization(GetIdentity().Name, out requestingClient, request);
        if (response != null)
        {
            return response.AsActionResult();
        }

        var model = new AuthorizeClientApplicationViewModel
        {
            ClientApplication = requestingClient.Name,
            Scope = request.Scope,
            AuthorizationRequest = request,
        };

        return View(model);
    }

    public virtual IIdentity GetIdentity()
    {
        return User.Identity;
    }
}

我想测试一下,每当第三方应用程序没有授权时,都会向用户弹出一个 View ,请求他授权该应用程序。为此我需要 mock :

  • _oAuthService.RequestClientAuthorization

我的测试方法的设置将如下所示:

var oAuthService = new Mock<IOAuthService>();
oAuthService.Setup(a => a.RequestClientAuthorization(userName, out client, pendingRequest)).Returns(new OutgoingWebResponse()); // DotNetOpenAuth doesn't allow me to do the **new OutgoingWebResponse**

PD:这个问题我只写了一个controller的方法,其实有3个,而且场景都差不多。

最佳答案

一种可能性是编写一个包装器(与 ASP.NET MVC 抽象所有 HTTP 上下文特定内容的方式相同):

public abstract class OutgoingWebResponseWrapperBase
{
    protected OutgoingWebResponseWrapperBase() { }

    public abstract ActionResult AsActionResult();
}

然后有一个简单的实现:

public class OutgoingWebResponseWrapper: OutgoingWebResponseWrapperBase
{
    private readonly OutgoingWebResponse _response;
    public OutgoingWebResponseWrapper(OutgoingWebResponse response)
    {
        _response = response;
    }

    public override ActionResult AsActionResult()
    {
        return _response.AsActionResult();
    }
}

现在修改 IOAuthService.RequestClientAuthorization 方法以返回 OutgoingWebResponseWrapperBase 而不是 OutgoingWebResponse

就这样:

public interface IOAuthService
{
    ...
    OutgoingWebResponseWrapperBase RequestClientAuthorization(...);
}

显然,您的 Controller 代码将保持完全相同。只是现在您可以在单元测试中模拟 RequestClientAuthorization 的返回类型,因为它是一个抽象类。您还可以模拟 AsActionResult 抽象方法调用以返回一些预期的模拟实例,并且您将在单元测试中断言您正在测试的 Controller 操作返回了这个预期的操作结果。

关于c# - 单元测试 dotnetopenauth ctp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913187/

相关文章:

c# - 使用 LINQ 将 Dictionary<Guid,IList<String>> 转换为 Dictionary<string,IList<Guid>>?

asp.net-mvc - 警告 - 项目更新后无法识别 html.helpers

c# - OAuthWebSecurity.RegisterMicrosoftClient 的错误请求 (400)

c# - 从 Google OpenID 迁移到新的 OAuth 2

c# - 如何使用 C# 在 xamarin android 中将字节数组转换为 pdf?

c# - 为什么我的 for 循环告诉我我正在将 int 转换为 bool?

c# - 如何知道是否正在使用 C# 播放声音?

javascript - VS2008 JavaScript 调试器 IE8 "there is no source code available for the current location"

c# - 如何在 C# 中构建搜索引擎

openid - 信息亭PC上的OpenID