Skip to content

Commit 83b5d00

Browse files
committed
Remove Moq usage from middleware tests
1 parent b97e664 commit 83b5d00

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

Diff for: Directory.Build.props

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
<PropertyGroup>
4040
<CoverletVersion>6.0.*</CoverletVersion>
4141
<GitHubActionsTestLoggerVersion>2.3.*</GitHubActionsTestLoggerVersion>
42-
<MoqVersion>4.18.*</MoqVersion>
4342
<TestSdkVersion>17.7.*</TestSdkVersion>
4443
</PropertyGroup>
4544
</Project>

Diff for: src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ public sealed class JsonApiMiddleware
2323
private static readonly MediaTypeHeaderValue MediaType = MediaTypeHeaderValue.Parse(HeaderConstants.MediaType);
2424
private static readonly MediaTypeHeaderValue AtomicOperationsMediaType = MediaTypeHeaderValue.Parse(HeaderConstants.AtomicOperationsMediaType);
2525

26-
private readonly RequestDelegate _next;
26+
private readonly RequestDelegate? _next;
2727

28-
public JsonApiMiddleware(RequestDelegate next, IHttpContextAccessor httpContextAccessor)
28+
public JsonApiMiddleware(RequestDelegate? next, IHttpContextAccessor httpContextAccessor)
2929
{
30+
ArgumentGuard.NotNull(httpContextAccessor);
31+
3032
_next = next;
3133

3234
var session = new AspNetCodeTimerSession(httpContextAccessor);
@@ -77,9 +79,12 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
7779
httpContext.RegisterJsonApiRequest();
7880
}
7981

80-
using (CodeTimingSessionManager.Current.Measure("Subsequent middleware"))
82+
if (_next != null)
8183
{
82-
await _next(httpContext);
84+
using (CodeTimingSessionManager.Current.Measure("Subsequent middleware"))
85+
{
86+
await _next(httpContext);
87+
}
8388
}
8489
}
8590

Diff for: test/UnitTests/Middleware/JsonApiRequestTests.cs renamed to test/JsonApiDotNetCoreTests/UnitTests/Middleware/JsonApiMiddlewareTests.cs

+34-13
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
using JsonApiDotNetCore.Resources.Annotations;
88
using Microsoft.AspNetCore.Http;
99
using Microsoft.AspNetCore.Http.Features;
10+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
1011
using Microsoft.AspNetCore.Mvc.Controllers;
1112
using Microsoft.Extensions.Logging.Abstractions;
12-
using Moq;
1313
using TestBuildingBlocks;
1414
using Xunit;
1515

1616
#pragma warning disable AV1561 // Signature contains too many parameters
1717

18-
namespace UnitTests.Middleware;
18+
namespace JsonApiDotNetCoreTests.UnitTests.Middleware;
1919

20-
public sealed class JsonApiRequestTests
20+
public sealed class JsonApiMiddlewareTests
2121
{
2222
// @formatter:wrap_lines false
2323
[Theory]
@@ -65,7 +65,7 @@ public async Task Sets_request_properties_correctly(string requestMethod, string
6565
var httpContext = new DefaultHttpContext();
6666
IControllerResourceMapping controllerResourceMapping = SetupRoutes(httpContext, resourceGraph, requestMethod, requestPath);
6767

68-
var middleware = new JsonApiMiddleware(_ => Task.CompletedTask, new HttpContextAccessor
68+
var middleware = new JsonApiMiddleware(null, new HttpContextAccessor
6969
{
7070
HttpContext = httpContext
7171
});
@@ -153,16 +153,10 @@ private static IControllerResourceMapping SetupRoutes(HttpContext httpContext, I
153153
ControllerTypeInfo = (TypeInfo)typeof(object)
154154
};
155155

156-
var controllerResourceMappingMock = new Mock<IControllerResourceMapping>();
156+
httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(controllerActionDescriptor), null));
157157

158-
controllerResourceMappingMock.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny<Type>())).Returns(() =>
159-
{
160-
return pathSegments.Length > 0 ? resourceGraph.GetResourceTypes().FirstOrDefault(resourceType => resourceType.PublicName == pathSegments[0]) : null;
161-
});
162-
163-
httpContext.SetEndpoint(new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(controllerActionDescriptor), null));
164-
165-
return controllerResourceMappingMock.Object;
158+
string? resourceTypePublicName = pathSegments.Length > 0 ? pathSegments[0] : null;
159+
return new FakeJsonApiRoutingConvention(resourceGraph, resourceTypePublicName);
166160
}
167161

168162
public enum IsReadOnly
@@ -198,4 +192,31 @@ private sealed class TodoItem : Identifiable<int>
198192
[HasMany]
199193
public ISet<ItemTag> Tags { get; set; } = new HashSet<ItemTag>();
200194
}
195+
196+
private sealed class FakeJsonApiRoutingConvention : IJsonApiRoutingConvention
197+
{
198+
private readonly IResourceGraph _resourceGraph;
199+
private readonly string? _resourceTypePublicName;
200+
201+
public FakeJsonApiRoutingConvention(IResourceGraph resourceGraph, string? resourceTypePublicName)
202+
{
203+
_resourceGraph = resourceGraph;
204+
_resourceTypePublicName = resourceTypePublicName;
205+
}
206+
207+
public void Apply(ApplicationModel application)
208+
{
209+
throw new NotImplementedException();
210+
}
211+
212+
public ResourceType? GetResourceTypeForController(Type? controllerType)
213+
{
214+
return _resourceTypePublicName != null ? _resourceGraph.FindResourceType(_resourceTypePublicName) : null;
215+
}
216+
217+
public string GetControllerNameForResourceType(ResourceType? resourceType)
218+
{
219+
throw new NotImplementedException();
220+
}
221+
}
201222
}

Diff for: test/UnitTests/UnitTests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
<PackageReference Include="GitHubActionsTestLogger" Version="$(GitHubActionsTestLoggerVersion)" PrivateAssets="All" />
1313
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="$(EFCoreVersion)" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
15-
<PackageReference Include="Moq" Version="$(MoqVersion)" />
1615
</ItemGroup>
1716
</Project>

0 commit comments

Comments
 (0)