Skip to content

Commit 9e4e9c8

Browse files
authored
Merge pull request #1338 from json-api-dotnet/remove-moq-usage
Remove Moq usage
2 parents a3418fe + 83b5d00 commit 9e4e9c8

File tree

7 files changed

+88
-72
lines changed

7 files changed

+88
-72
lines changed

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>

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

src/JsonApiDotNetCore/Properties/AssemblyInfo.cs

-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
[assembly: InternalsVisibleTo("Benchmarks")]
44
[assembly: InternalsVisibleTo("JsonApiDotNetCoreTests")]
55
[assembly: InternalsVisibleTo("UnitTests")]
6-
[assembly: InternalsVisibleTo("DiscoveryTests")]
7-
[assembly: InternalsVisibleTo("TestBuildingBlocks")]

test/DiscoveryTests/DiscoveryTests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="All" />
1313
<PackageReference Include="GitHubActionsTestLogger" Version="$(GitHubActionsTestLoggerVersion)" PrivateAssets="All" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
15-
<PackageReference Include="Moq" Version="$(MoqVersion)" />
1615
</ItemGroup>
1716
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using FluentAssertions;
22
using JsonApiDotNetCore.Configuration;
3-
using JsonApiDotNetCore.Middleware;
4-
using JsonApiDotNetCore.Queries;
53
using JsonApiDotNetCore.Repositories;
64
using JsonApiDotNetCore.Resources;
75
using JsonApiDotNetCore.Services;
@@ -10,53 +8,33 @@
108
using Microsoft.Extensions.DependencyInjection;
119
using Microsoft.Extensions.Logging;
1210
using Microsoft.Extensions.Logging.Abstractions;
13-
using Moq;
1411
using TestBuildingBlocks;
1512
using Xunit;
1613

1714
namespace DiscoveryTests;
1815

1916
public sealed class ServiceDiscoveryFacadeTests
2017
{
21-
private static readonly ILoggerFactory LoggerFactory = NullLoggerFactory.Instance;
22-
private readonly IServiceCollection _services = new ServiceCollection();
23-
private readonly ResourceGraphBuilder _resourceGraphBuilder;
18+
private readonly ServiceCollection _services = new();
2419

2520
public ServiceDiscoveryFacadeTests()
2621
{
27-
var dbResolverMock = new Mock<IDbContextResolver>();
28-
dbResolverMock.Setup(resolver => resolver.GetContext()).Returns(new Mock<DbContext>().Object);
29-
_services.AddScoped(_ => dbResolverMock.Object);
30-
31-
IJsonApiOptions options = new JsonApiOptions();
32-
33-
_services.AddSingleton(options);
34-
_services.AddSingleton(LoggerFactory);
35-
_services.AddScoped(_ => new Mock<IJsonApiRequest>().Object);
36-
_services.AddScoped(_ => new Mock<ITargetedFields>().Object);
37-
_services.AddScoped(_ => new Mock<IResourceGraph>().Object);
38-
_services.AddScoped(typeof(IResourceChangeTracker<>), typeof(ResourceChangeTracker<>));
39-
_services.AddScoped(_ => new Mock<IResourceFactory>().Object);
40-
_services.AddScoped(_ => new Mock<IPaginationContext>().Object);
41-
_services.AddScoped(_ => new Mock<IQueryLayerComposer>().Object);
42-
_services.AddScoped(_ => new Mock<IResourceRepositoryAccessor>().Object);
43-
_services.AddScoped(_ => new Mock<IResourceDefinitionAccessor>().Object);
44-
45-
_resourceGraphBuilder = new ResourceGraphBuilder(options, LoggerFactory);
22+
_services.AddSingleton<ILoggerFactory>(_ => NullLoggerFactory.Instance);
23+
_services.AddScoped<IDbContextResolver>(_ => new FakeDbContextResolver());
4624
}
4725

4826
[Fact]
4927
public void Can_add_resources_from_assembly_to_graph()
5028
{
5129
// Arrange
52-
var facade = new ServiceDiscoveryFacade(_services, _resourceGraphBuilder, LoggerFactory);
53-
facade.AddAssembly(typeof(Person).Assembly);
30+
Action<ServiceDiscoveryFacade> addAction = facade => facade.AddAssembly(typeof(Person).Assembly);
5431

5532
// Act
56-
facade.DiscoverResources();
33+
_services.AddJsonApi(discovery: facade => addAction(facade));
5734

5835
// Assert
59-
IResourceGraph resourceGraph = _resourceGraphBuilder.Build();
36+
ServiceProvider serviceProvider = _services.BuildServiceProvider();
37+
var resourceGraph = serviceProvider.GetRequiredService<IResourceGraph>();
6038

6139
ResourceType? personType = resourceGraph.FindResourceType(typeof(Person));
6240
personType.ShouldNotBeNull();
@@ -69,67 +47,84 @@ public void Can_add_resources_from_assembly_to_graph()
6947
public void Can_add_resource_from_current_assembly_to_graph()
7048
{
7149
// Arrange
72-
var facade = new ServiceDiscoveryFacade(_services, _resourceGraphBuilder, LoggerFactory);
73-
facade.AddCurrentAssembly();
50+
Action<ServiceDiscoveryFacade> addAction = facade => facade.AddCurrentAssembly();
7451

7552
// Act
76-
facade.DiscoverResources();
53+
_services.AddJsonApi(discovery: facade => addAction(facade));
7754

7855
// Assert
79-
IResourceGraph resourceGraph = _resourceGraphBuilder.Build();
56+
ServiceProvider serviceProvider = _services.BuildServiceProvider();
57+
var resourceGraph = serviceProvider.GetRequiredService<IResourceGraph>();
8058

81-
ResourceType? testResourceType = resourceGraph.FindResourceType(typeof(PrivateResource));
82-
testResourceType.ShouldNotBeNull();
59+
ResourceType? resourceType = resourceGraph.FindResourceType(typeof(PrivateResource));
60+
resourceType.ShouldNotBeNull();
8361
}
8462

8563
[Fact]
8664
public void Can_add_resource_service_from_current_assembly_to_container()
8765
{
8866
// Arrange
89-
var facade = new ServiceDiscoveryFacade(_services, _resourceGraphBuilder, LoggerFactory);
90-
facade.AddCurrentAssembly();
67+
Action<ServiceDiscoveryFacade> addAction = facade => facade.AddCurrentAssembly();
9168

9269
// Act
93-
facade.DiscoverInjectables();
70+
_services.AddJsonApi(discovery: facade => addAction(facade));
9471

9572
// Assert
96-
ServiceProvider services = _services.BuildServiceProvider();
73+
ServiceProvider serviceProvider = _services.BuildServiceProvider();
74+
var resourceService = serviceProvider.GetRequiredService<IResourceService<PrivateResource, int>>();
9775

98-
var resourceService = services.GetRequiredService<IResourceService<PrivateResource, int>>();
9976
resourceService.Should().BeOfType<PrivateResourceService>();
10077
}
10178

10279
[Fact]
10380
public void Can_add_resource_repository_from_current_assembly_to_container()
10481
{
10582
// Arrange
106-
var facade = new ServiceDiscoveryFacade(_services, _resourceGraphBuilder, LoggerFactory);
107-
facade.AddCurrentAssembly();
83+
Action<ServiceDiscoveryFacade> addAction = facade => facade.AddCurrentAssembly();
10884

10985
// Act
110-
facade.DiscoverInjectables();
86+
_services.AddJsonApi(discovery: facade => addAction(facade));
11187

11288
// Assert
113-
ServiceProvider services = _services.BuildServiceProvider();
89+
ServiceProvider serviceProvider = _services.BuildServiceProvider();
90+
var resourceRepository = serviceProvider.GetRequiredService<IResourceRepository<PrivateResource, int>>();
11491

115-
var resourceRepository = services.GetRequiredService<IResourceRepository<PrivateResource, int>>();
11692
resourceRepository.Should().BeOfType<PrivateResourceRepository>();
11793
}
11894

11995
[Fact]
12096
public void Can_add_resource_definition_from_current_assembly_to_container()
12197
{
12298
// Arrange
123-
var facade = new ServiceDiscoveryFacade(_services, _resourceGraphBuilder, LoggerFactory);
124-
facade.AddCurrentAssembly();
99+
Action<ServiceDiscoveryFacade> addAction = facade => facade.AddCurrentAssembly();
125100

126101
// Act
127-
facade.DiscoverInjectables();
102+
_services.AddJsonApi(discovery: facade => addAction(facade));
128103

129104
// Assert
130-
ServiceProvider services = _services.BuildServiceProvider();
105+
ServiceProvider serviceProvider = _services.BuildServiceProvider();
106+
var resourceDefinition = serviceProvider.GetRequiredService<IResourceDefinition<PrivateResource, int>>();
131107

132-
var resourceDefinition = services.GetRequiredService<IResourceDefinition<PrivateResource, int>>();
133108
resourceDefinition.Should().BeOfType<PrivateResourceDefinition>();
134109
}
110+
111+
private sealed class FakeDbContextResolver : IDbContextResolver
112+
{
113+
private readonly FakeDbContextOptions _dbContextOptions = new();
114+
115+
public DbContext GetContext()
116+
{
117+
return new DbContext(_dbContextOptions);
118+
}
119+
120+
private sealed class FakeDbContextOptions : DbContextOptions
121+
{
122+
public override Type ContextType => typeof(object);
123+
124+
public override DbContextOptions WithExtension<TExtension>(TExtension extension)
125+
{
126+
return this;
127+
}
128+
}
129+
}
135130
}

test/UnitTests/Middleware/JsonApiRequestTests.cs 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
}

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)