单元测试Web服务 - HttpContext

时间:2010-10-26 21:09:28

标签: asp.net unit-testing httpcontext

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

在测试的上下文中,没有HttpContext这样的东西,所以测试总是失败。这些Web服务应该如何进行单元测试?

5 个答案:

答案 0 :(得分:25)

Here是一个相关的讨论。

我直接停止引用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()来自here,如下所示:

    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;
    }

它使用名为mocking framework

moq

在测试项目中,您必须添加对System.WebSystem.Web.Abstractions的引用,其中HttpContextBase已定义。

答案 1 :(得分:2)

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

interface IAuthenticator
{
   bool IsAuthenticated();
}

并实施真实的:

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

但是在测试中,创建一个mock并返回true或false:

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

答案 2 :(得分:1)

您可能会考虑依赖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来测试SomeOperationNeedingAuthorization。

答案 3 :(得分:0)

我最终在网络服务上添加了一个属性:

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

然后在我的网络服务方法中:

<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)

答案 4 :(得分:0)

基于上面的解决方案,我在O2 Platform中实现了一个包装类,允许轻松使用这些模拟类,例如,这是我可以从HttpRequest.InputStream中编写和读取的方法。 p>

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/