单元测试 Web 服务 - HttpContext

15

我想为一个Web服务编写单元测试。我创建了我的测试项目,引用了我的Web项目(不是服务引用,而是程序集引用),然后编写一些代码来测试Web服务- 它们可以正常工作。但是,有些服务通过使用HttpContext.Current.User.Identity.IsAuthenticated确保用户已登录到Web应用程序。

在测试环境中,不存在HttpContext这样的东西,因此测试总是失败。这种类型的Web服务应该如何进行单元测试?

5个回答

26

这里有一个相关的讨论。

我停止直接引用HttpContext.Current,使用了这个类代替:

public class HttpContextFactory
{
    private static HttpContextBase m_context;
    public static HttpContextBase Current
    {
        get
        {
            if (m_context != null)
                return m_context;

            if (HttpContext.Current == null)
                throw new InvalidOperationException("HttpContext not available");

            return new HttpContextWrapper(HttpContext.Current);
        }
    }

    public static void SetCurrentContext(HttpContextBase context)
    {
        m_context = context;
    }
}

在我们的代码中,使用 HttpContextFactory.Current 代替 HttpContext.Current

然后你可以在你的测试中写入这个:

        HttpContextFactory.SetCurrentContext(GetMockedHttpContext());

GetMockedHttpContext()是从这里获取的,并且看起来像这样:

    private System.Web.HttpContextBase GetMockedHttpContext()
    {
        var context = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        var response = new Mock<HttpResponseBase>();
        var session = new Mock<HttpSessionStateBase>();
        var server = new Mock<HttpServerUtilityBase>();
        var user = new Mock<IPrincipal>();     
        var identity = new Mock<IIdentity>();

        context.Setup(ctx => ctx.Request).Returns(request.Object);
        context.Setup(ctx => ctx.Response).Returns(response.Object);
        context.Setup(ctx => ctx.Session).Returns(session.Object);
        context.Setup(ctx => ctx.Server).Returns(server.Object);
        context.Setup(ctx => ctx.User).Returns(user.Object);
        user.Setup(x => x.Identity).Returns(identity.Object);
        identity.Setup(id => id.IsAuthenticated).Returns(true);
        identity.Setup(id => id.Name).Returns("test");

        return context.Object;
    }

它使用一个名为 moq模拟框架

在您的测试项目中,您需要添加对 System.WebSystem.Web.Abstractions 的引用,其中定义了 HttpContextBase


我也开始这样做(用HttpContextFactory替换HttpContext),这真的有助于单元测试(我已经将这些功能封装在API中,您可以在这里查看http://o2platform.wordpress.com/2011/04/05/mocking-httpcontext-httprequest-and-httpresponse-for-unittests-using-moq/)。 - Dinis Cruz
1
我没有使用moq,所以这并没有让我达到100%,但它还是很有帮助的。我查看了Stephen Walther的虚拟对象以获取帮助:http://stephenwalther.com/archive/2008/07/01/asp-net-mvc-tip-12-faking-the-controller-context.aspx - Don Rolling

3

如果你正在使用模拟技术,你可以将此逻辑包装在另一个类中:

interface IAuthenticator
{
   bool IsAuthenticated();
}

并实现真正的目标:

class Authenticator : IAuthenticator
{
   bool IsAuthenticated()
   {
      return HttpContext.Current.User.Identity.IsAuthenticated;
   }
}

但在测试中,需要创建一个模拟对象并返回 true 或 false:

Mock<IAuthenticator> mock = new Mock<IAuthenticator>();
mock.Expect(x => x.IsAuthenticated()).Returns(true);

要绝对正确,我认为这实际上应该是一个存根而不是模拟。 - Jakob Christensen
你的意思是我们需要一个存根而不是模拟吗?我不同意,因为基于是否经过身份验证,服务的行为会有所不同,因此我们需要期望能够返回。我不确定 - 也不是那么热衷于 - 存根和模拟的区别,但对我来说,那是一个模拟而不是存根。 - Aliostad
如果你想要测试 IAuthenticator 接口是否被正确调用,那么应该使用 mock。如果你希望测试其他内容,则应使用 stub。Stubs 永远不会导致测试失败,它们只是为了使事情顺利运行。无论如何,我猜这取决于您的mocking框架。在 Rhino Mocks 中,mocks 和 stubs 之间存在微妙的区别:https://dev59.com/0XRB5IYBdhLWcg3w7riV - Jakob Christensen

2

您可以考虑依赖于System.Web.Abstractions.HttpContextBase,而不是使用HttpContext.Current。 System.Web.Abstractions程序集已经为您封装了许多常见的ASP.NET Http *类。它们在整个ASP.NET MVC代码中被广泛使用。如果您正在使用IoC / DI框架,则很容易使用。例如,在Ninject中:

Bind<HttpContextBase>.ToMethod(x => new HttpContextWrapper(HttpContext.Current));

然后在你的构造函数中...

public class SomeWebService
{
    private HttpContextBase _httpContext;

    public SomeWebService(HttpContextBase httpContext)
    {
        _httpContext = httpContext;
    }

    public void SomeOperationNeedingAuthorization()
    {
        IIdentity userIdentity = _httpContext.User.Identity;

        if (!userIdentity.IsAuthenticated)
            return;

        // Do something here...
    }
}

这样说有点过于简化了,但我希望你能明白我的意思... 正如Aliostad所提到的那样,您可以使用Rhino Mocks或Moq等工具轻松模拟HttpContextBase来测试需要授权的某些操作。


您可能还考虑使用商业模拟工具,例如Typemock Isolator或Telerik的JustMock,这可以减轻使用/管理Http*抽象的负担。 - Andy S

1
最终我在Web服务上放置了一个属性:
Private mIdentity As System.Security.Principal.IIdentity
Public Property Identity() As System.Security.Principal.IIdentity
  Get
    If mIdentity Is Nothing Then mIdentity = HttpContext.Current.User.Identity
    Return mIdentity
  End Get
  Set(ByVal value As System.Security.Principal.IIdentity)
    mIdentity = value
  End Set
End Property

然后在我的 Web 服务方法中:

<WebMethod()> _
Public Function GetProject(ByVal projectId As Int32) As String

  If Me.Identity.IsAuthenticated Then

    'code here

  End If

End Function

然后在我的测试中(我正在使用RhinoMocks):

Dim mockery As New MockRepository()
Dim mockIdentity As System.Security.Principal.IIdentity = mockery.DynamicMock(Of System.Security.Principal.IIdentity)()

Dim projectService As New TeamDynamix.Enterprise.Web.Next.ProjectService()
projectService.Identity = mockIdentity
mockIdentity.Stub(Function(i As System.Security.Principal.IIdentity) i.IsAuthenticated).Return(True)

0

基于上面的解决方案,我在O2平台中实现了一个包装类,使得使用这些模拟类变得非常容易。例如,这就是我如何从HttpRequest.InputStream中读写数据的方式。

var mockHttpContext = new API_Moq_HttpContext();
var httpContext = mockHttpContext.httpContext();
httpContext.request_Write("<html><body>".line());
httpContext.request_Write("   this is a web page".line());  
httpContext.request_Write("</body></html>"); 
return httpContext.request_Read();

请查看此博客文章以获取更多详细信息:http://o2platform.wordpress.com/2011/04/05/mocking-httpcontext-httprequest-and-httpresponse-for-unittests-using-moq/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接