Skip to content

Commit

Permalink
misc: Removed dependency to MediatR from UnitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhartmann123 authored and davidroth committed Nov 16, 2023
1 parent 7cabeab commit 439d668
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Email/test/Email.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="netDumbster" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
Expand Down
7 changes: 7 additions & 0 deletions src/Email/test/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Diagnostics;
using Fusonic.Extensions.UnitTests;
using Fusonic.Extensions.UnitTests.SimpleInjector;
using MediatR;

namespace Fusonic.Extensions.Email.Tests;

Expand All @@ -17,4 +19,9 @@ public abstract class TestBase<TFixture> : DependencyInjectionUnitTest<TFixture>
{
protected TestBase(TFixture fixture) : base(fixture)
{ }

/// <summary> Runs a mediator command in its own scope. Used to reduce possible side effects from test data creation and the like. </summary>
[DebuggerStepThrough]
protected Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
=> ScopedAsync(() => GetInstance<IMediator>().Send(request));
}
7 changes: 7 additions & 0 deletions src/Hangfire/test/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Diagnostics;
using Fusonic.Extensions.UnitTests.EntityFrameworkCore;
using MediatR;

namespace Fusonic.Extensions.Hangfire.Tests;

Expand All @@ -16,4 +18,9 @@ public abstract class TestBase<TFixture> : DatabaseUnitTest<TestDbContext, TFixt
{
protected TestBase(TFixture fixture) : base(fixture)
{ }

/// <summary> Runs a mediator command in its own scope. Used to reduce possible side effects from test data creation and the like. </summary>
[DebuggerStepThrough]
protected Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
=> ScopedAsync(() => GetInstance<IMediator>().Send(request));
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
18 changes: 3 additions & 15 deletions src/UnitTests/src/DependencyInjectionUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Diagnostics;
using MediatR;
using Xunit;

namespace Fusonic.Extensions.UnitTests;

public abstract class DependencyInjectionUnitTest<TFixture> : IDisposable, IClassFixture<TFixture>
public abstract class DependencyInjectionUnitTest<TFixture>(TFixture fixture) : IDisposable, IClassFixture<TFixture>
where TFixture : class, IDependencyInjectionTestFixture
{
private object currentScope;
protected TFixture Fixture { get; }

protected DependencyInjectionUnitTest(TFixture fixture)
{
Fixture = fixture;
currentScope = fixture.BeginScope();
}
private object currentScope = fixture.BeginScope();
protected TFixture Fixture { get; } = fixture;

/// <summary> Gets an instance of the requested service. </summary>
[DebuggerStepThrough]
Expand All @@ -28,11 +21,6 @@ protected T GetInstance<T>()
[DebuggerStepThrough]
protected object GetInstance(Type serviceType) => Fixture.GetInstance(currentScope, serviceType);

/// <summary> Runs a mediator command in its own scope. Used to reduce possible side effects from test data creation and the like. </summary>
[DebuggerStepThrough]
protected Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
=> ScopedAsync(() => GetInstance<IMediator>().Send(request));

/// <summary> <see cref="ScopedAsync{TResult}"/> </summary>
[DebuggerStepThrough]
protected TResult Scoped<TResult>(Func<TResult> action)
Expand Down
3 changes: 0 additions & 3 deletions src/UnitTests/src/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MediatR" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="SimpleInjector" />
<PackageReference Include="xunit" />
</ItemGroup>

Expand Down

0 comments on commit 439d668

Please sign in to comment.