1
1
using FluentAssertions ;
2
2
using JsonApiDotNetCore . Configuration ;
3
- using JsonApiDotNetCore . Middleware ;
4
- using JsonApiDotNetCore . Queries ;
5
3
using JsonApiDotNetCore . Repositories ;
6
4
using JsonApiDotNetCore . Resources ;
7
5
using JsonApiDotNetCore . Services ;
10
8
using Microsoft . Extensions . DependencyInjection ;
11
9
using Microsoft . Extensions . Logging ;
12
10
using Microsoft . Extensions . Logging . Abstractions ;
13
- using Moq ;
14
11
using TestBuildingBlocks ;
15
12
using Xunit ;
16
13
17
14
namespace DiscoveryTests ;
18
15
19
16
public sealed class ServiceDiscoveryFacadeTests
20
17
{
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 ( ) ;
24
19
25
20
public ServiceDiscoveryFacadeTests ( )
26
21
{
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 ( ) ) ;
46
24
}
47
25
48
26
[ Fact ]
49
27
public void Can_add_resources_from_assembly_to_graph ( )
50
28
{
51
29
// 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 ) ;
54
31
55
32
// Act
56
- facade . DiscoverResources ( ) ;
33
+ _services . AddJsonApi ( discovery : facade => addAction ( facade ) ) ;
57
34
58
35
// Assert
59
- IResourceGraph resourceGraph = _resourceGraphBuilder . Build ( ) ;
36
+ ServiceProvider serviceProvider = _services . BuildServiceProvider ( ) ;
37
+ var resourceGraph = serviceProvider . GetRequiredService < IResourceGraph > ( ) ;
60
38
61
39
ResourceType ? personType = resourceGraph . FindResourceType ( typeof ( Person ) ) ;
62
40
personType . ShouldNotBeNull ( ) ;
@@ -69,67 +47,84 @@ public void Can_add_resources_from_assembly_to_graph()
69
47
public void Can_add_resource_from_current_assembly_to_graph ( )
70
48
{
71
49
// Arrange
72
- var facade = new ServiceDiscoveryFacade ( _services , _resourceGraphBuilder , LoggerFactory ) ;
73
- facade . AddCurrentAssembly ( ) ;
50
+ Action < ServiceDiscoveryFacade > addAction = facade => facade . AddCurrentAssembly ( ) ;
74
51
75
52
// Act
76
- facade . DiscoverResources ( ) ;
53
+ _services . AddJsonApi ( discovery : facade => addAction ( facade ) ) ;
77
54
78
55
// Assert
79
- IResourceGraph resourceGraph = _resourceGraphBuilder . Build ( ) ;
56
+ ServiceProvider serviceProvider = _services . BuildServiceProvider ( ) ;
57
+ var resourceGraph = serviceProvider . GetRequiredService < IResourceGraph > ( ) ;
80
58
81
- ResourceType ? testResourceType = resourceGraph . FindResourceType ( typeof ( PrivateResource ) ) ;
82
- testResourceType . ShouldNotBeNull ( ) ;
59
+ ResourceType ? resourceType = resourceGraph . FindResourceType ( typeof ( PrivateResource ) ) ;
60
+ resourceType . ShouldNotBeNull ( ) ;
83
61
}
84
62
85
63
[ Fact ]
86
64
public void Can_add_resource_service_from_current_assembly_to_container ( )
87
65
{
88
66
// Arrange
89
- var facade = new ServiceDiscoveryFacade ( _services , _resourceGraphBuilder , LoggerFactory ) ;
90
- facade . AddCurrentAssembly ( ) ;
67
+ Action < ServiceDiscoveryFacade > addAction = facade => facade . AddCurrentAssembly ( ) ;
91
68
92
69
// Act
93
- facade . DiscoverInjectables ( ) ;
70
+ _services . AddJsonApi ( discovery : facade => addAction ( facade ) ) ;
94
71
95
72
// Assert
96
- ServiceProvider services = _services . BuildServiceProvider ( ) ;
73
+ ServiceProvider serviceProvider = _services . BuildServiceProvider ( ) ;
74
+ var resourceService = serviceProvider . GetRequiredService < IResourceService < PrivateResource , int > > ( ) ;
97
75
98
- var resourceService = services . GetRequiredService < IResourceService < PrivateResource , int > > ( ) ;
99
76
resourceService . Should ( ) . BeOfType < PrivateResourceService > ( ) ;
100
77
}
101
78
102
79
[ Fact ]
103
80
public void Can_add_resource_repository_from_current_assembly_to_container ( )
104
81
{
105
82
// Arrange
106
- var facade = new ServiceDiscoveryFacade ( _services , _resourceGraphBuilder , LoggerFactory ) ;
107
- facade . AddCurrentAssembly ( ) ;
83
+ Action < ServiceDiscoveryFacade > addAction = facade => facade . AddCurrentAssembly ( ) ;
108
84
109
85
// Act
110
- facade . DiscoverInjectables ( ) ;
86
+ _services . AddJsonApi ( discovery : facade => addAction ( facade ) ) ;
111
87
112
88
// Assert
113
- ServiceProvider services = _services . BuildServiceProvider ( ) ;
89
+ ServiceProvider serviceProvider = _services . BuildServiceProvider ( ) ;
90
+ var resourceRepository = serviceProvider . GetRequiredService < IResourceRepository < PrivateResource , int > > ( ) ;
114
91
115
- var resourceRepository = services . GetRequiredService < IResourceRepository < PrivateResource , int > > ( ) ;
116
92
resourceRepository . Should ( ) . BeOfType < PrivateResourceRepository > ( ) ;
117
93
}
118
94
119
95
[ Fact ]
120
96
public void Can_add_resource_definition_from_current_assembly_to_container ( )
121
97
{
122
98
// Arrange
123
- var facade = new ServiceDiscoveryFacade ( _services , _resourceGraphBuilder , LoggerFactory ) ;
124
- facade . AddCurrentAssembly ( ) ;
99
+ Action < ServiceDiscoveryFacade > addAction = facade => facade . AddCurrentAssembly ( ) ;
125
100
126
101
// Act
127
- facade . DiscoverInjectables ( ) ;
102
+ _services . AddJsonApi ( discovery : facade => addAction ( facade ) ) ;
128
103
129
104
// Assert
130
- ServiceProvider services = _services . BuildServiceProvider ( ) ;
105
+ ServiceProvider serviceProvider = _services . BuildServiceProvider ( ) ;
106
+ var resourceDefinition = serviceProvider . GetRequiredService < IResourceDefinition < PrivateResource , int > > ( ) ;
131
107
132
- var resourceDefinition = services . GetRequiredService < IResourceDefinition < PrivateResource , int > > ( ) ;
133
108
resourceDefinition . Should ( ) . BeOfType < PrivateResourceDefinition > ( ) ;
134
109
}
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
+ }
135
130
}
0 commit comments