elsa-core/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs

59 lines
2.4 KiB
C#
Raw Normal View History

2020-10-19 20:12:11 +00:00
using System;
using System.Threading.Tasks;
2020-10-23 10:40:33 +00:00
using Elsa.Builders;
using Elsa.Persistence.InMemory;
2020-10-19 20:12:11 +00:00
using Elsa.Services;
using Elsa.Testing.Shared.Helpers;
2020-10-23 10:40:33 +00:00
using Elsa.Triggers;
2020-10-19 20:12:11 +00:00
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;
namespace Elsa.Testing.Shared.Unit
2020-10-19 20:12:11 +00:00
{
2020-11-13 19:47:08 +00:00
public abstract class WorkflowsUnitTestBase : IAsyncLifetime, IDisposable
2020-10-19 20:12:11 +00:00
{
private readonly TemporaryFolder _tempFolder;
2020-11-13 19:47:08 +00:00
protected WorkflowsUnitTestBase(ITestOutputHelper testOutputHelper, Action<IServiceCollection>? configureServices = default)
2020-10-19 20:12:11 +00:00
{
_tempFolder = new TemporaryFolder();
TestOutputHelper = testOutputHelper;
var services = new ServiceCollection()
.AddElsa()
2020-10-19 20:12:11 +00:00
.AddConsoleActivities(Console.In, new XunitConsoleForwarder(testOutputHelper));
configureServices?.Invoke(services);
ServiceProvider = services.BuildServiceProvider();
ServiceScope = ServiceProvider.CreateScope();
WorkflowRunner = ServiceScope.ServiceProvider.GetRequiredService<IWorkflowRunner>();
WorkflowBlueprintMaterializer = ServiceScope.ServiceProvider.GetRequiredService<IWorkflowBlueprintMaterializer>();
WorkflowBuilder = ServiceScope.ServiceProvider.GetRequiredService<IWorkflowBuilder>();
WorkflowRegistry = ServiceScope.ServiceProvider.GetRequiredService<IWorkflowRegistry>();
WorkflowSelector = ServiceScope.ServiceProvider.GetRequiredService<IWorkflowSelector>();
2020-10-19 20:12:11 +00:00
}
protected ITestOutputHelper TestOutputHelper { get; }
protected ServiceProvider ServiceProvider { get; }
protected IServiceScope ServiceScope { get; }
2020-10-23 10:40:33 +00:00
protected IWorkflowRunner WorkflowRunner { get; }
protected IWorkflowBlueprintMaterializer WorkflowBlueprintMaterializer { get; }
protected IWorkflowBuilder WorkflowBuilder { get; }
protected IWorkflowRegistry WorkflowRegistry { get; }
protected IWorkflowSelector WorkflowSelector { get; }
2020-10-19 20:12:11 +00:00
public virtual void Dispose() => _tempFolder.Dispose();
public virtual async Task InitializeAsync()
{
var startupRunner = ServiceProvider.GetRequiredService<IStartupRunner>();
await startupRunner.StartupAsync();
}
public virtual async Task DisposeAsync()
{
await ServiceProvider.DisposeAsync();
Dispose();
}
}
}