Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Represents the **NuGet** versions.

## v5.9.0
- *Enhancement:* Added `WithGenericTester` (_MSTest_ and _NUnit_ only) class to enable class-level generic tester usage versus one-off.
- *Enhancement:* Added `TesterBase.UseScopedTypeSetUp()` to enable a function that will be executed directly before each `ScopedTypeTester{TService}` is instantiated to allow standardized/common set up to occur.

## v5.8.0
- *Enhancement:* Extended the `MockHttpClientResponse.With*` methods to support optional _media type_ parameter to enable specification of the `Content-Type` header value.
- *Enhancement:* Added `HttpResponseMessageAssertor.AssertContentTypeProblemJson` to enable asserting that the content type is `application/problem+json`.
Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>5.8.0</Version>
<Version>5.9.0</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
51 changes: 51 additions & 0 deletions src/UnitTestEx.MSTest/WithGenericTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/UnitTestEx

using System;
using UnitTestEx.Generic;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace UnitTestEx
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
/// <summary>
/// Provides a shared <see cref="Test"/> <see cref="GenericTester{TEntryPoint}"/> to enable usage of the same underlying instance across multiple tests.
/// </summary>
/// <typeparam name="TEntryPoint">The generic startup <see cref="Type"/>.</typeparam>
/// <remarks>Implements <see cref="IDisposable"/> to automatically dispose of the <see cref="Test"/> instance to release all resources.
/// <para>Be aware that using may result in cross-test contamination.</para></remarks>
public abstract class WithGenericTester<TEntryPoint> : IDisposable where TEntryPoint : class
{
private bool _disposed;
private GenericTester<TEntryPoint>? _genericTester = GenericTester.Create<TEntryPoint>();

/// <summary>
/// Gets the underlying <see cref="GenericTester{TEntryPoint}"/> for testing.
/// </summary>
public GenericTester<TEntryPoint> Test => _genericTester ?? throw new ObjectDisposedException(nameof(Test));

/// <summary>
/// Dispose of all resources.
/// </summary>
/// <param name="disposing">Indicates whether from the <see cref="Dispose()"/>.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_genericTester?.Dispose();
_genericTester = null;
}

_disposed = true;
}
}

/// <inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
51 changes: 51 additions & 0 deletions src/UnitTestEx.NUnit/WithGenericTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/UnitTestEx

using System;
using UnitTestEx.Generic;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace UnitTestEx
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
/// <summary>
/// Provides a shared <see cref="Test"/> <see cref="GenericTester{TEntryPoint}"/> to enable usage of the same underlying instance across multiple tests.
/// </summary>
/// <typeparam name="TEntryPoint">The generic startup <see cref="Type"/>.</typeparam>
/// <remarks>Implements <see cref="IDisposable"/> to automatically dispose of the <see cref="Test"/> instance to release all resources.
/// <para>Be aware that using may result in cross-test contamination.</para></remarks>
public abstract class WithGenericTester<TEntryPoint> : IDisposable where TEntryPoint : class
{
private bool _disposed;
private GenericTester<TEntryPoint>? _genericTester = GenericTester.Create<TEntryPoint>();

/// <summary>
/// Gets the underlying <see cref="GenericTester{TEntryPoint}"/> for testing.
/// </summary>
public GenericTester<TEntryPoint> Test => _genericTester ?? throw new ObjectDisposedException(nameof(Test));

/// <summary>
/// Dispose of all resources.
/// </summary>
/// <param name="disposing">Indicates whether from the <see cref="Dispose()"/>.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_genericTester?.Dispose();
_genericTester = null;
}

_disposed = true;
}
}

/// <inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
29 changes: 29 additions & 0 deletions src/UnitTestEx/Abstractions/TesterBaseT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Moq;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnitTestEx.Hosting;
Expand All @@ -18,6 +19,8 @@ namespace UnitTestEx.Abstractions
/// <typeparam name="TSelf">The <see cref="TesterBase{TSelf}"/> to support inheriting fluent-style method-chaining.</typeparam>
public abstract class TesterBase<TSelf> : TesterBase where TSelf : TesterBase<TSelf>
{
private Func<IServiceProvider, Task>? _scopedSetUpAsync;

/// <summary>
/// Initializes a new instance of the <see cref="TesterBase{TSelf}"/> class.
/// </summary>
Expand Down Expand Up @@ -116,6 +119,17 @@ public TSelf UseAdditionalConfiguration(IEnumerable<KeyValuePair<string, string?
/// <remarks>Usage will result in a <see cref="TesterBase.ResetHost()"/>.</remarks>
public TSelf UseAdditionalConfiguration(string key, string? value) => UseAdditionalConfiguration([new KeyValuePair<string, string?>(key, value)]);

/// <summary>
/// Updates (replaces) the function that will be executed directly before each <see cref="ScopedTypeTester{TService}"/> is instantiated to allow standardized/common set up to occur.
/// </summary>
/// <param name="setupAsync">The set-up function.</param>
/// <returns>The <typeparamref name="TSelf"/> to support fluent-style method-chaining.</returns>
public TSelf UseScopedTypeSetUp(Func<IServiceProvider, Task> setupAsync)
{
_scopedSetUpAsync = setupAsync;
return (TSelf)this;
}

/// <summary>
/// Resets the underlying host to instantiate a new instance.
/// </summary>
Expand Down Expand Up @@ -426,6 +440,7 @@ public TSelf ScopedType<TService>(Action<ScopedTypeTester<TService>> scopedTeste
ArgumentNullException.ThrowIfNull(scopedTester);

using var scope = HostExecutionWrapper(Services.CreateScope);
InvokeScopedTesterSetUp(scope);
var tester = new ScopedTypeTester<TService>(this, scope.ServiceProvider, scope.ServiceProvider.CreateInstance<TService>(serviceKey));
scopedTester(tester);
return (TSelf)this;
Expand All @@ -442,6 +457,7 @@ public TSelf ScopedType<TService>(Func<IServiceProvider, TService> serviceFactor
{
ArgumentNullException.ThrowIfNull(scopedTester);
using var scope = HostExecutionWrapper(Services.CreateScope);
InvokeScopedTesterSetUp(scope);
var tester = new ScopedTypeTester<TService>(this, scope.ServiceProvider, serviceFactory(scope.ServiceProvider));
scopedTester(tester);
return (TSelf)this;
Expand All @@ -462,6 +478,7 @@ public TSelf ScopedType<TService>(Func<ScopedTypeTester<TService>, Task> scopedT
ArgumentNullException.ThrowIfNull(scopedTesterAsync);

using var scope = HostExecutionWrapper(Services.CreateScope);
InvokeScopedTesterSetUp(scope);
var tester = new ScopedTypeTester<TService>(this, scope.ServiceProvider, scope.ServiceProvider.CreateInstance<TService>(serviceKey));
scopedTesterAsync(tester).GetAwaiter().GetResult();
return (TSelf)this;
Expand All @@ -481,6 +498,7 @@ public TSelf ScopedType<TService>(Func<IServiceProvider, TService> serviceFactor
{
ArgumentNullException.ThrowIfNull(scopedTesterAsync);
using var scope = HostExecutionWrapper(Services.CreateScope);
InvokeScopedTesterSetUp(scope);
var tester = new ScopedTypeTester<TService>(this, scope.ServiceProvider, serviceFactory(scope.ServiceProvider));
scopedTesterAsync(tester).GetAwaiter().GetResult();
return (TSelf)this;
Expand All @@ -500,6 +518,7 @@ public TSelf ScopedType<TService>(Func<ScopedTypeTester<TService>, ValueTask> sc
ArgumentNullException.ThrowIfNull(scopedTesterAsync);

using var scope = HostExecutionWrapper(Services.CreateScope);
InvokeScopedTesterSetUp(scope);
var tester = new ScopedTypeTester<TService>(this, scope.ServiceProvider, scope.ServiceProvider.CreateInstance<TService>(serviceKey));
scopedTesterAsync(tester).AsTask().GetAwaiter().GetResult();
return (TSelf)this;
Expand All @@ -517,11 +536,21 @@ public TSelf ScopedType<TService>(Func<IServiceProvider, TService> serviceFactor
{
ArgumentNullException.ThrowIfNull(scopedTesterAsync);
using var scope = HostExecutionWrapper(Services.CreateScope);
InvokeScopedTesterSetUp(scope);
var tester = new ScopedTypeTester<TService>(this, scope.ServiceProvider, serviceFactory(scope.ServiceProvider));
scopedTesterAsync(tester).AsTask().GetAwaiter().GetResult();
return (TSelf)this;
}

#endif

/// <summary>
/// Executes the scoped tester set up.
/// </summary>
private void InvokeScopedTesterSetUp(IServiceScope? scope)
{
if (scope is not null && _scopedSetUpAsync is not null)
_scopedSetUpAsync(scope.ServiceProvider).GetAwaiter().GetResult();
}
}
}
28 changes: 28 additions & 0 deletions tests/UnitTestEx.MSTest.Test/Other/WithGenericTesterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestEx.MSTest.Test.Other
{
[TestClass]
public class WithGenericTesterTest : WithGenericTester<object>
{
[TestMethod]
public void Run_Success()
{
var c = Test.Services.GetService<IConfiguration>();

Test.Run(() => 1)
.AssertSuccess()
.AssertValue(1);
}

[TestMethod]
public void Run_Success_AssertJSON()
{
Test.Run(() => 1)
.AssertSuccess()
.AssertJson("1");
}
}
}
5 changes: 4 additions & 1 deletion tests/UnitTestEx.NUnit.Test/Other/GenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public class EntryPoint

//public void ConfigureServices(IServiceCollection services) { }

public void ConfigureApplication(IHostApplicationBuilder builder) => builder.Services.AddSingleton<Gin>();
public void ConfigureApplication(IHostApplicationBuilder builder)
{
builder.Services.AddSingleton<Gin>();
}
}
}
27 changes: 27 additions & 0 deletions tests/UnitTestEx.NUnit.Test/Other/WithGenericTesterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace UnitTestEx.NUnit.Test.Other
{
public class WithGenericTesterTest : WithGenericTester<EntryPoint>
{
[Test]
public void Run_Success()
{
var c = Test.Services.GetService<IConfiguration>();

Test.Run(() => 1)
.AssertSuccess()
.AssertValue(1);
}

[Test]
public void Run_Success_AssertJSON()
{
Test.Run(() => 1)
.AssertSuccess()
.AssertJson("1");
}
}
}
Loading