Skip to content

Commit b97e664

Browse files
committed
Remove Moq usage from discovery tests
1 parent a3418fe commit b97e664

File tree

3 files changed

+45
-53
lines changed

3 files changed

+45
-53
lines changed

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
}

0 commit comments

Comments
 (0)