這個問題是相當多的ASP.NET MVC unit testing custom AuthorizeAttribute重複 - 但我不能讓我的代碼沒有擊中同一個NullReferenceException
單元測試自定義MVC AuthorizationAttribute
東西顯然是失蹤,當我嘲笑了我的控制器上運行。
我的屬性很簡單 - 是這樣的:
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (!base.AuthorizeCore(filterContext.HttpContext))
{
filterContext.Controller.TempData.Add("RedirectReason", "Unauthorized");
}
base.OnAuthorization(filterContext);
}
我最近一次嘗試是在下面。我也嘗試了引用鏈接上的問題代碼,包括向上下文中添加項目。
// Arrange
Mock<IPrincipal> userMock = new Mock<IPrincipal>();
Mock<HttpResponseBase> responseMock = new Mock<HttpResponseBase>();
responseMock.SetupGet(res => res.Cache).Returns(new Mock<HttpCachePolicyBase>().Object);
Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>(MockBehavior.Strict);
contextMock.SetupGet(cm => cm.User).Returns(userMock.Object);
contextMock.SetupGet(cm => cm.Response).Returns(responseMock.Object);
contextMock.SetupGet(cm => cm.Items).Returns(new Mock<IDictionary>().Object);
Mock<ControllerContext> controllerContextMock = new Mock<ControllerContext>();
controllerContextMock.SetupGet(ctx => ctx.HttpContext).Returns(contextMock.Object);
Mock<ActionDescriptor> actionDesMock = new Mock<ActionDescriptor>();
actionDesMock.Setup(action => action.GetCustomAttributes(typeof(AllowAnonymousAttribute), true)).Returns(new object[0]);
// Act
CustomAuthorizationAttribute cusAtr = new CustomAuthorizationAttribute() { Roles = "TestRole" };
// Assert
cusAtr.OnAuthorization(new AuthorizationContext(controllerContextMock.Object, actionDesMock.Object));
我也檢查了AuthorizeCore的源代碼。
當跑,在測試上base.AuthorizeCore(filterContext.HttpContext)
失敗了NullReferenceException
想不通有什麼遺漏。
有什麼想法嗎?
爲了方便起見,代碼從另一個問題,調整,以適應我們的模型是:
// Arrange
var httpContext = new Mock<HttpContextBase>();
Dictionary<object, object> dictionary = new Dictionary<object, object>();
httpContext.SetupGet(cm => cm.Items).Returns(dictionary);
Mock<IPrincipal> userMock = new Mock<IPrincipal>();
httpContext.SetupGet(c => c.User).Returns(() => userMock.Object);
var requestBase = new Mock<HttpRequestBase>();
var headers = new NameValueCollection
{
{"Special-Header-Name", "false"}
};
requestBase.Setup(x => x.Headers).Returns(headers);
requestBase.Setup(x => x.HttpMethod).Returns("GET");
requestBase.Setup(x => x.Url).Returns(new Uri("http://localhost/"));
requestBase.Setup(x => x.RawUrl).Returns("~/Maintenance/UnExistingMaster");
requestBase.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(() => "~/Maintenance/UnExistingMaster");
requestBase.Setup(x => x.IsAuthenticated).Returns(() => true);
httpContext.Setup(x => x.Request).Returns(requestBase.Object);
var controller = new Mock<ControllerBase>();
var actionDescriptor = new Mock<ActionDescriptor>();
var controllerContext = new ControllerContext(httpContext.Object, new RouteData(), controller.Object);
CustomAuthorizationAttribute cusAtr = new CustomAuthorizationAttribute() { Roles = "TestRole" };
// Act
cusAtr.OnAuthorization(new AuthorizationContext(controllerContext, actionDescriptor.Object));
提供可用於重現問題的[mcve]。幫助我們來幫助你。不得不去檢查另一個問題,嘗試拼湊你的屬性進行測試,這使得幫助變得更加困難。不要以爲我們已經有了關聯問題的背景。準確識別出錯位置。這個問題應該能夠自立。 – Nkosi
@Nkosi - 你介意讓我知道我錯過了什麼嗎?我已經提供了我對這個問題的每一段代碼,以及我遇到的問題。不知道我還能添加什麼?該屬性位於問題的頂部。我的測試是在底部。我引用另一個問題,因此讀取知道可能有重複;而不是讓用戶試圖根據重複來關閉Q. – Darren