Skip to content

Commit 5545a91

Browse files
committed
test(service-collection-extension): test service injection
1 parent 19a3822 commit 5545a91

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/JsonApiDotNetCore/Extensions/ServiceProviderExtensions.cs renamed to src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace JsonApiDotNetCore.Extensions
1414
{
15-
public static class ServiceProviderExtensions
15+
public static class IServiceCollectionExtensions
1616
{
1717
public static void AddJsonApi<TContext>(this IServiceCollection services)
1818
where TContext : DbContext
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Xunit;
2+
using JsonApiDotNetCore.Builders;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using JsonApiDotNetCore.Extensions;
5+
using JsonApiDotNetCore.Configuration;
6+
using Microsoft.EntityFrameworkCore;
7+
using JsonApiDotNetCore.Data;
8+
using JsonApiDotNetCore.Internal;
9+
using Microsoft.AspNetCore.Http;
10+
using JsonApiDotNetCore.Services;
11+
using JsonApiDotNetCoreExample.Data;
12+
using Microsoft.Extensions.Caching.Memory;
13+
using JsonApiDotNetCoreExample.Models;
14+
15+
namespace JsonApiDotNetCoreExampleTests.Unit.Extensions
16+
{
17+
public class IServiceCollectionExtensionsTests
18+
{
19+
[Fact]
20+
public void AddJsonApiInternals_Adds_All_Required_Services()
21+
{
22+
// arrange
23+
var services = new ServiceCollection();
24+
var jsonApiOptions = new JsonApiOptions();
25+
26+
services.AddDbContext<AppDbContext>(options =>
27+
{
28+
options.UseMemoryCache(new MemoryCache(new MemoryCacheOptions()));
29+
}, ServiceLifetime.Transient);
30+
31+
// act
32+
services.AddJsonApiInternals<AppDbContext>(jsonApiOptions);
33+
var provider = services.BuildServiceProvider();
34+
35+
// assert
36+
Assert.NotNull(provider.GetService<DbContext>());
37+
Assert.NotNull(provider.GetService(typeof(IEntityRepository<TodoItem>)));
38+
Assert.NotNull(provider.GetService<JsonApiOptions>());
39+
Assert.NotNull(provider.GetService<IContextGraph>());
40+
Assert.NotNull(provider.GetService<IJsonApiContext>());
41+
Assert.NotNull(provider.GetService<IHttpContextAccessor>());
42+
Assert.NotNull(provider.GetService<IMetaBuilder>());
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)