0
我弄不明白爲什麼這個單元測試不起作用。它是基於關碼樣本中發現這裏: http://weblogs.asp.net/rashid/archive/2009/03/12/unit-testable-httpmodule-and-httphandler.aspx單元測試http模塊,查http狀態代碼
[Test]
public void when_user_doesnt_authenticate_returns_304_status()
{
_httpContext = new Mock<HttpContextBase>();
_httpRequest = new Mock<HttpRequestBase>();
_httpResponse = new Mock<HttpResponseBase>();
_httpContext.SetupGet(context => context.Request).Returns(_httpRequest.Object);
var module = new TAS.HttpModule.TASHttpModule();
_httpResponse.SetupSet(response => response.StatusCode = 304).Verifiable();
_httpContext.SetupGet(context => context.Response).Returns(_httpResponse.Object);
module.OnAuthenticateRequest(_httpContext.Object);
_httpResponse.VerifyAll();
}
從http模塊代碼(簡化的證明問題)
public class TASHttpModule : BaseHttpModule
public override void OnAuthenticateRequest(HttpContextBase context)
{
HttpResponseBase response = context.Response;
response.StatusCode = 403;
response.StatusDescription = "Access Denied";
response.End();
}
}
public class BaseHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) => OnBeginRequest(new HttpContextWrapper(((HttpApplication)sender).Context));
context.Error += (sender, e) => OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
context.EndRequest += (sender, e) => OnEndRequest(new HttpContextWrapper(((HttpApplication)sender).Context));
context.AuthenticateRequest += (sender, e) => OnAuthenticateRequest(new HttpContextWrapper(((HttpApplication)sender).Context));
}
public void Dispose()
{
}
public virtual void OnBeginRequest(HttpContextBase context)
{
}
public virtual void OnError(HttpContextBase context)
{
}
public virtual void OnEndRequest(HttpContextBase context)
{
}
public virtual void OnAuthenticateRequest(HttpContextBase context)
{
}
}
的基本問題是,response.StatusCode值不能被設置。它總是回覆到零。因此,檢查304狀態的測試總是失敗。任何幫助將不勝感激!