|
3 | 3 | using Microsoft.Extensions.DependencyInjection; |
4 | 4 | using Microsoft.Extensions.Logging; |
5 | 5 | using NSubstitute; |
| 6 | +using Serilog; |
| 7 | +using Serilog.Sinks.XUnit3; |
6 | 8 |
|
7 | 9 | namespace HydraScript.IntegrationTests; |
8 | 10 |
|
@@ -31,39 +33,40 @@ public void Dispose() |
31 | 33 |
|
32 | 34 | public Runner GetRunner(Options options, Action<IServiceCollection>? configureTestServices = null) |
33 | 35 | { |
34 | | - var serviceProvider = Program.GetServiceProvider( |
35 | | - new FileInfo(options.FileName), |
36 | | - options.Dump, |
37 | | - services => |
| 36 | + var services = new ServiceCollection() |
| 37 | + .AddDomain() |
| 38 | + .AddApplication() |
| 39 | + .AddInfrastructure(options.Dump, new FileInfo(options.FileName)); |
| 40 | + const string serilogTemplate = "[{Timestamp:HH:mm:ss} {Level:u} [{SourceContext}]] {Message:lj} {Exception}"; |
| 41 | + services.AddLogging(x => x.ClearProviders() |
| 42 | + .AddSerilog(new LoggerConfiguration().WriteTo.XUnit3TestOutput(serilogTemplate).CreateLogger()) |
| 43 | + .AddFakeLogging(fakeLogOptions => |
38 | 44 | { |
39 | | - services.AddLogging(x => x.ClearProviders() |
40 | | - .AddXUnit(new ImplicitTestOutputHelperAccessor()) |
41 | | - .AddFakeLogging(fakeLogOptions => |
| 45 | + fakeLogOptions.OutputSink = logMessage => _logMessages.Add(logMessage); |
| 46 | + fakeLogOptions.OutputFormatter = fakeLogRecord => |
| 47 | + fakeLogRecord.Level switch |
42 | 48 | { |
43 | | - fakeLogOptions.OutputSink = logMessage => _logMessages.Add(logMessage); |
44 | | - fakeLogOptions.OutputFormatter = fakeLogRecord => |
45 | | - fakeLogRecord.Level switch |
46 | | - { |
47 | | - LogLevel.Error => $"{fakeLogRecord.Message} {fakeLogRecord.Exception?.Message}", |
48 | | - _ => fakeLogRecord.ToString() |
49 | | - }; |
50 | | - })); |
| 49 | + LogLevel.Error => $"{fakeLogRecord.Message} {fakeLogRecord.Exception?.Message}", |
| 50 | + _ => fakeLogRecord.ToString() |
| 51 | + }; |
| 52 | + })); |
51 | 53 |
|
52 | | - if (options.MockFileSystem) |
53 | | - { |
54 | | - var fileSystem = Substitute.For<IFileSystem>(); |
55 | | - services.AddSingleton(fileSystem); |
56 | | - } |
| 54 | + if (options.MockFileSystem) |
| 55 | + { |
| 56 | + var fileSystem = Substitute.For<IFileSystem>(); |
| 57 | + services.AddSingleton(fileSystem); |
| 58 | + } |
| 59 | + |
| 60 | + if (!string.IsNullOrWhiteSpace(options.InMemoryScript)) |
| 61 | + { |
| 62 | + var sourceCodeProvider = Substitute.For<ISourceCodeProvider>(); |
| 63 | + sourceCodeProvider.GetText().ReturnsForAnyArgs(options.InMemoryScript); |
| 64 | + services.AddSingleton(sourceCodeProvider); |
| 65 | + } |
57 | 66 |
|
58 | | - if (!string.IsNullOrWhiteSpace(options.InMemoryScript)) |
59 | | - { |
60 | | - var sourceCodeProvider = Substitute.For<ISourceCodeProvider>(); |
61 | | - sourceCodeProvider.GetText().ReturnsForAnyArgs(options.InMemoryScript); |
62 | | - services.AddSingleton(sourceCodeProvider); |
63 | | - } |
| 67 | + configureTestServices?.Invoke(services); |
64 | 68 |
|
65 | | - configureTestServices?.Invoke(services); |
66 | | - }); |
| 69 | + var serviceProvider = services.BuildServiceProvider(); |
67 | 70 | var executor = serviceProvider.GetRequiredService<Executor>(); |
68 | 71 | return new Runner(serviceProvider, executor); |
69 | 72 | } |
|
0 commit comments