| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Akka.Actor; |
| | 5 | | using Akka.Actor.Setup; |
| | 6 | | using Akka.TestKit.Xunit2; |
| | 7 | | using Akka.Util.Internal; |
| | 8 | | using Microsoft.Extensions.Hosting; |
| | 9 | | using SeungYongShim.Akka.DependencyInjection; |
| | 10 | | using SeungYongShim.Akka.DependencyInjection.Abstractions; |
| | 11 | |
|
| | 12 | | namespace Microsoft.Extensions.DependencyInjection |
| | 13 | | { |
| | 14 | | public delegate IActorRef GetTestActor(); |
| | 15 | |
|
| | 16 | | public static class UseAkkaTestKitExtensions |
| | 17 | | { |
| | 18 | | public static IHostBuilder UseAkkaWithXUnit2(this IHostBuilder host) => |
| 6 | 19 | | host.UseAkkaWithXUnit2(default); |
| | 20 | |
|
| | 21 | | public static IHostBuilder UseAkkaWithXUnit2(this IHostBuilder host, |
| | 22 | | params Type [] mockType) => |
| 7 | 23 | | host.ConfigureServices((context, services) => |
| 14 | 24 | | services.AddAkkaTestKit(mockType ?? Enumerable.Empty<Type>())); |
| | 25 | |
|
| | 26 | | private static IServiceCollection AddAkkaTestKit(this IServiceCollection services, IEnumerable<Type> mocks) |
| 7 | 27 | | { |
| 14 | 28 | | services.AddSingleton(sp => new TestKit(sp.GetService<ActorSystemSetup>())) |
| 19 | 29 | | .AddSingleton<GetTestActor>(sp => () => sp.GetService<TestKit>().TestActor) |
| 14 | 30 | | .AddSingleton(sp => sp.GetService<TestKit>().Sys); |
| | 31 | |
|
| 8 | 32 | | mocks.ForEach(x => services.AddSingleton(typeof(IPropsFactory<>).MakeGenericType(x), |
| 8 | 33 | | typeof(FakePropsFactory<>).MakeGenericType(x))); |
| | 34 | |
|
| 7 | 35 | | return services; |
| 7 | 36 | | } |
| | 37 | | } |
| | 38 | | } |