使用HttpHandler进行模拟,单元测试(NUnit)设置问题

时间:2010-08-24 22:19:55

标签: unit-testing nunit mocking moq httphandler

这篇文章与另外两篇文章herehere有关。

我是Unit Testing&的新手。惩戒。我有一个测试夹具试图模拟一个包含响应和请求的HttpContext对象。我认为测试代码设置不正确,因为在调用处理程序后我立即收到错误。我得到的错误是:

UnitTests.UADHandlerFixture.Request_Is_Object: System.NullReferenceException:未将对象引用设置为对象的实例。

在BaseHttpHandler.cs中的Abstract.BaseHttpHandler.get_Request():第21行

在BaseHttpHandler.cs中的Abstract.BaseHttpHandler.get_IsRequestFromUAD()中:第23行

at UADTimeHttpHandler.cs中的Handlers.UADTimeHttpHandler.ProcessRequest(HttpContextBase context):第19行

在UADHttpHanderTests.cs的UnitTests.UADHandlerFixture.Request_Is_Object()中:第47行

测试代码是这样的:

    [TestFixture]
public class UADHandlerFixture
{
    private Mock<HttpContextBase> _mockHttpContext;
    private Mock<HttpRequestBase> _mockHttpRequest;
    private Mock<HttpResponseBase> _mockHttpResponse;
    private UADTimeHttpHandler _handler;
    private WindsorContainer _container;

    [SetUp]
    public void Init()
    {
        _mockHttpRequest = new Mock<HttpRequestBase>();
        _mockHttpResponse = new Mock<HttpResponseBase>();
        _mockHttpContext = new Mock<HttpContextBase>();
        _container = new WindsorContainer();
        _container.AddComponent<ILogger, FakeLogger>();
        _container.AddComponent<IDataRepository, FakeDataRepository>();

        _mockHttpContext.SetupGet(x => x.Application[0]).Returns(_container);
        _mockHttpContext.SetupGet(x => x.Request).Returns(_mockHttpRequest.Object);
        _mockHttpContext.SetupGet(x => x.Response).Returns(_mockHttpResponse.Object);

        _handler = new UADTimeHttpHandler();
    }

    [Test]
    public void Request_Is_Object()
    {
        _handler.ProcessRequest(_mockHttpContext.Object);
    }
}

处理程序:

    public class UADTimeHttpHandler : BaseHttpHandler
{
    public override void ProcessRequest(HttpContextBase context)
    {
        if (IsRequestFromUAD)
        {
            Logger.Log("Log stuff");
            DataRepository.Write("DB stuff");
            ReturnResponse(HttpStatusCode.OK, DateTime.Now.ToString());
        }
        else
            ReturnResponse(HttpStatusCode.BadRequest);
    }
}

BaseHandler:

public abstract class BaseHttpHandler : IHttpHandler
{
    private HttpContext _httpContext;
    private ILogger _logger;
    private IDataRepository _dataRepository;
    protected ILogger Logger { get { return _logger; } }
    protected IDataRepository DataRepository { get { return _dataRepository; } }
    protected HttpContext Context { get { return _httpContext; } }
    protected HttpRequest Request { get { return _httpContext.Request; } }
    protected HttpResponse Response { get { return _httpContext.Response; } }
    protected bool IsRequestFromUAD { get { return Request.UserAgent == null ? false : Request.UserAgent.Equals("UAD"); } }
    public virtual bool IsReusable { get { return false; } }

    public void ProcessRequest(HttpContext context)
    {
        WindsorContainer container = (WindsorContainer)context.Application[0];
        _logger = container.Resolve<ILogger>();
        _dataRepository = container.Resolve<IDataRepository>();

        _httpContext = context;
        ProcessRequest(new HttpContextWrapper(context));
    }

    protected virtual void ReturnResponse(HttpStatusCode httpStatus)
    {
        Response.StatusCode = (int)httpStatus;
    }

    protected virtual void ReturnResponse(HttpStatusCode httpStatus, string response)
    {
        Response.StatusCode = (int)httpStatus;
        Response.Write(response);
    }

    protected virtual string GetInputStream()
    {
        using (var reader = new StreamReader(Request.InputStream))
        {
            return reader.ReadToEnd();
        }
    }

    public abstract void ProcessRequest(HttpContextBase context);
}

1 个答案:

答案 0 :(得分:1)

我误读了你的帖子。无视这个答案。

我很好奇您在存储库中调用Dispose()的位置。