I have a unit test trying to mock the generation of an HttpResponseMessage. It fails with what appears to be an internal JustMock error, but I'm not sure what to try. If anybody can recommend a solution or has seen anything similar, I'd appreciate the help. Thanks!
Here's the test:
There is an error in "OnAuthorization", which just calls HttpRequestMessage.CreateErrorResponse. Here is the error:
Here's the test:
var actionContext = Mock.Create<
HttpActionContext
>();
var actionDescriptor = Mock.Create<
HttpActionDescriptor
>();
actionContext.ActionDescriptor = actionDescriptor;
actionDescriptor.Arrange(a => a.GetCustomAttributes<
AllowAnonymousAttribute
>())
.Returns(new Collection<
AllowAnonymousAttribute
>());
var controllerContext = Mock.Create<
HttpControllerContext
>();
actionContext.ControllerContext = controllerContext;
var controllerDescriptor = Mock.Create<
HttpControllerDescriptor
>();
controllerContext.ControllerDescriptor = controllerDescriptor;
controllerDescriptor.Arrange(a => a.GetCustomAttributes<
AllowAnonymousAttribute
>())
.Returns(new Collection<
AllowAnonymousAttribute
>());
var requestContext = Mock.Create<
HttpRequestContext
>();
controllerContext.RequestContext = requestContext;
var request = Mock.Create<
HttpRequestMessage
>();
request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
actionContext.Arrange(x => x.Request).Returns(request);
var principal = Mock.Create<
IPrincipal
>();
requestContext.Principal = principal;
var identity = Mock.Create<
IIdentity
>();
principal.Arrange(x => x.Identity).Returns(identity);
identity.Arrange(x => x.IsAuthenticated).Returns(true);
var attribute = new PermissionsAttribute("TEST_ROLE");
attribute.OnAuthorization(actionContext);
Assert.IsNotNull(actionContext.Response);
Assert.AreEqual(HttpStatusCode.Forbidden, actionContext.Response.StatusCode);
There is an error in "OnAuthorization", which just calls HttpRequestMessage.CreateErrorResponse. Here is the error:
Test method Tests.Filters.PermissionsAttributeTests.Test_403 threw exception: <
br
>System.NullReferenceException: Object reference not set to an instance of an object.<
br
> at System.Net.Http.Headers.MediaTypeHeaderValue.GetHashCode()<
br
> at System.ValueType.GetHashCode()<
br
> at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)<
br
> at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)<
br
> at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)<
br
> at Telerik.JustMock.Core.Behaviors.RecursiveMockingBehavior.Process(Invocation invocation)<
br
> at Telerik.JustMock.Core.MocksRepository.DispatchInvocation(Invocation invocation)<
br
> at Telerik.JustMock.Core.ProfilerInterceptor.Intercept(RuntimeTypeHandle typeHandle, RuntimeMethodHandle methodHandle, Object[] data)<
br
> at System.Net.Http.Headers.MediaTypeHeaderValue.get_MediaType()<
br
> at System.Net.Http.Formatting.ParsedMediaTypeHeaderValue..ctor(MediaTypeHeaderValue mediaType)<
br
> at System.Net.Http.Formatting.MediaTypeHeaderValueExtensions.IsSubsetOf(MediaTypeHeaderValue mediaType1, MediaTypeHeaderValue mediaType2, ref MediaTypeHeaderValueRange mediaType2Range)<
br
> at System.Net.Http.Formatting.MediaTypeHeaderValueExtensions.IsSubsetOf(MediaTypeHeaderValue mediaType1, MediaTypeHeaderValue mediaType2)<
br
> at System.Net.Http.Formatting.DefaultContentNegotiator.MatchRequestMediaType(HttpRequestMessage request, MediaTypeFormatter formatter)<
br
> at System.Net.Http.Formatting.DefaultContentNegotiator.ComputeFormatterMatches(Type type, HttpRequestMessage request, IEnumerable`1 formatters)<
br
> at System.Net.Http.Formatting.DefaultContentNegotiator.Negotiate(Type type, HttpRequestMessage request, IEnumerable`1 formatters)<
br
> at System.Web.Http.Results.NegotiatedContentResult`1.Execute(HttpStatusCode statusCode, T content, IContentNegotiator contentNegotiator, HttpRequestMessage request, IEnumerable`1 formatters)<
br
> at System.Net.Http.HttpRequestMessageExtensions.CreateResponse(HttpRequestMessage request, HttpStatusCode statusCode, T value, HttpConfiguration configuration)<
br
> at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func`2 errorCreator)<
br
> at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, HttpError error)<
br
> at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, String message)<
br
> at PermissionsAttribute.HandleUnauthorizedRequest(HttpActionContext actionContext) in PermissionsAttribute.cs: line 48<
br
> at System.Web.Http.AuthorizeAttribute.OnAuthorization(HttpActionContext actionContext)<
br
> at Tests.Filters.PermissionsAttributeTests.Test_403() in PermissionsAttributeTests.cs: line 91