Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v5.2.0 #81

Merged
merged 2 commits into from
Jan 20, 2025
Merged
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
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
7.0.x
8.0.x
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v5.2.0
- *Enhancement:* Added `TesterBase<TSelf>.UseAdditionalConfiguration` method to enable additional configuration to be specified that overrides the `IHostBuilder` as the host is being built. This leverages the underlying `IConfigurationBuilder.AddInMemoryCollection` capability to add. This is intended to support additional configuration that is not part of the standard `appsettings.json` or `appsettings.unittest.json` configuration.

## v5.1.0
- *Enhancement:* Where an `HttpRequest` is used for an Azure Functions `HttpTriggerTester` the passed `HttpRequest.PathAndQuery` is checked against that defined by the corresponding `HttpTriggerAttribute.Route` and will result in an error where different. The `HttpTrigger.WithRouteChecK` and `WithNoRouteCheck` methods control the path and query checking as needed.

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.1.0</Version>
<Version>5.2.0</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@ public abstract class FunctionTesterBase<TEntryPoint, TSelf> : TesterBase<TSelf>

private readonly bool? _includeUnitTestConfiguration;
private readonly bool? _includeUserSecrets;
private readonly IEnumerable<KeyValuePair<string, string?>>? _additionalConfiguration;
private IHost? _host;
private bool _disposed;

@@ -62,7 +61,7 @@ public FunctionTesterBase(TestFrameworkImplementor implementor, bool? includeUni
Logger = LoggerProvider.CreateLogger(GetType().Name);
_includeUnitTestConfiguration = includeUnitTestConfiguration;
_includeUserSecrets = includeUserSecrets;
_additionalConfiguration = additionalConfiguration;
AdditionalConfiguration = additionalConfiguration;
}

/// <summary>
@@ -168,8 +167,8 @@ private IHost GetHost()
if (!_includeUnitTestConfiguration.HasValue && TestSetUp.FunctionTesterIncludeUnitTestConfiguration || _includeUnitTestConfiguration.HasValue && _includeUnitTestConfiguration.Value)
cb.AddJsonFile("appsettings.unittest.json", optional: true);

if (_additionalConfiguration != null)
cb.AddInMemoryCollection(_additionalConfiguration);
if (AdditionalConfiguration != null)
cb.AddInMemoryCollection(AdditionalConfiguration);
})
.ConfigureServices(sc =>
{
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<RootNamespace>UnitTestEx</RootNamespace>
<Product>UnitTestEx</Product>
<Title>UnitTestEx Azure Functions Test Extensions.</Title>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<RootNamespace>UnitTestEx</RootNamespace>
<Product>UnitTestEx</Product>
<Title>UnitTestEx Azure Functions Test Extensions.</Title>
2 changes: 1 addition & 1 deletion src/UnitTestEx.MSTest/UnitTestEx.MSTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<RootNamespace>UnitTestEx.MSTest</RootNamespace>
<Product>UnitTestEx MSTest</Product>
<Title>UnitTestEx MSTest Test Extensions.</Title>
2 changes: 1 addition & 1 deletion src/UnitTestEx.NUnit/UnitTestEx.NUnit.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<RootNamespace>UnitTestEx.NUnit</RootNamespace>
<Product>UnitTestEx NUnit</Product>
<Title>UnitTestEx NUnit Test Extensions.</Title>
2 changes: 1 addition & 1 deletion src/UnitTestEx.Xunit/UnitTestEx.Xunit.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<RootNamespace>UnitTestEx.Xunit</RootNamespace>
<Product>UnitTestEx Xunit</Product>
<Title>UnitTestEx Xunit Test Extensions.</Title>
17 changes: 16 additions & 1 deletion src/UnitTestEx/Abstractions/TesterBase.cs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnitTestEx.Json;
using UnitTestEx.Logging;

@@ -18,6 +19,7 @@ public abstract class TesterBase
{
private string? _userName;
private readonly List<Action<IServiceCollection>> _configureServices = [];
private IEnumerable<KeyValuePair<string, string?>>? _additionalConfiguration;

/// <summary>
/// Static constructor.
@@ -97,6 +99,19 @@ public string UserName
protected set => _userName = value;
}

/// <summary>
/// Gets the additional configuration used at host initialization (see <see cref="MemoryConfigurationBuilderExtensions.AddInMemoryCollection(IConfigurationBuilder, IEnumerable{KeyValuePair{string, string}})"/>).
/// </summary>
public IEnumerable<KeyValuePair<string, string?>>? AdditionalConfiguration
{
get => _additionalConfiguration?.ToArray();
protected set
{
_additionalConfiguration = value;
ResetHost(false);
}
}

/// <summary>
/// Gets the <see cref="IConfiguration"/> from the underlying host.
/// </summary>
@@ -182,7 +197,7 @@ public void ConfigureServices(Action<IServiceCollection> configureServices, bool
protected void AddConfiguredServices(IServiceCollection services)
{
if (IsHostInstantiated)
throw new InvalidOperationException($"Underlying host has been instantiated and as such the {nameof(ConfigureServices)} operations can no longer be used.");
throw new InvalidOperationException($"Underlying host has been instantiated and as such the {nameof(ConfigureServices)} operations can no longer be used; consider using '{nameof(ResetHost)}' prior to enable.");

foreach (var configureService in _configureServices)
{
23 changes: 23 additions & 0 deletions src/UnitTestEx/Abstractions/TesterBaseT.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/UnitTestEx

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using System.Collections.Generic;
using UnitTestEx.Json;

namespace UnitTestEx.Abstractions
@@ -90,6 +92,27 @@ public TSelf UseJsonComparerOptions(JsonElementComparerOptions options)
return (TSelf)this;
}

/// <summary>
/// Updates (replaces) the <see cref="TesterBase.AdditionalConfiguration"/> (see <see cref="MemoryConfigurationBuilderExtensions.AddInMemoryCollection(IConfigurationBuilder, IEnumerable{KeyValuePair{string, string}})"/>).
/// </summary>
/// <param name="additionalConfiguration">The additional configuration key/value pairs.</param>
/// <returns>The <typeparamref name="TSelf"/> to support fluent-style method-chaining.</returns>
/// <remarks>Usage will result in a <see cref="TesterBase.ResetHost()"/>.</remarks>
public TSelf UseAdditionalConfiguration(IEnumerable<KeyValuePair<string, string?>>? additionalConfiguration)
{
AdditionalConfiguration = additionalConfiguration;
return (TSelf)this;
}

/// <summary>
/// Updates (replaces) the <see cref="TesterBase.AdditionalConfiguration"/> (see <see cref="MemoryConfigurationBuilderExtensions.AddInMemoryCollection(IConfigurationBuilder, IEnumerable{KeyValuePair{string, string}})"/>) with specified <paramref name="key"/> and <paramref name="value"/>.
/// </summary>
/// <param name="key">The additional configuration key.</param>
/// <param name="value">The additional configuration value.</param>
/// <returns>The <typeparamref name="TSelf"/> to support fluent-style method-chaining.</returns>
/// <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>
/// Resets the underlying host to instantiate a new instance.
/// </summary>
7 changes: 6 additions & 1 deletion src/UnitTestEx/AspNetCore/ApiTesterBase.cs
Original file line number Diff line number Diff line change
@@ -61,7 +61,12 @@ protected WebApplicationFactory<TEntryPoint> GetWebApplicationFactory()

return _waf = new WebApplicationFactory<TEntryPoint>().WithWebHostBuilder(whb =>
whb.UseSolutionRelativeContentRoot(Environment.CurrentDirectory)
.ConfigureAppConfiguration((_, x) => x.AddJsonFile("appsettings.unittest.json", optional: true))
.ConfigureAppConfiguration((_, cb) =>
{
cb.AddJsonFile("appsettings.unittest.json", optional: true);
if (AdditionalConfiguration != null)
cb.AddInMemoryCollection(AdditionalConfiguration);
})
.ConfigureServices(sc =>
{
sc.AddHttpContextAccessor();
3 changes: 3 additions & 0 deletions src/UnitTestEx/Generic/GenericTesterCore.cs
Original file line number Diff line number Diff line change
@@ -67,6 +67,9 @@ private IHost GetHost()
ep.ConfigureAppConfiguration(hbc, cb);
cb.AddJsonFile("appsettings.unittest.json", optional: true)
.AddEnvironmentVariables();

if (AdditionalConfiguration != null)
cb.AddInMemoryCollection(AdditionalConfiguration);
})
.ConfigureServices(sc =>
{
13 changes: 12 additions & 1 deletion tests/UnitTestEx.NUnit.Test/Other/GenericTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System;
using System.Threading.Tasks;
@@ -51,6 +52,16 @@ public void Run_Service()
test.Run<Gin>(gin => gin.StirAsync())
.AssertException<DivideByZeroException>("As required by Bond; shaken, not stirred.");
}

[Test]
public void Configuration_Overrride_Use()
{
// Demonstrates how to override the configuration settings for a test.
using var test = GenericTester.Create();
test.UseAdditionalConfiguration([new("SpecialKey", "NotSoSpecial")]);
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Just for testing aye bro!")]
15 changes: 15 additions & 0 deletions tests/UnitTestEx.NUnit.Test/ProductControllerTest.cs
Original file line number Diff line number Diff line change
@@ -101,6 +101,21 @@ public void Configuration()
Assert.That(cv, Is.EqualTo("OtherValue"));
}

[Test]
public void Configuration_Use()
{
using var test = ApiTester.Create<Startup>();
test.UseAdditionalConfiguration("SpecialKey", "NotSoSpecial");

var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));

// Null to reset.
test.UseAdditionalConfiguration(null);
cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("VerySpecialValue"));
}

[Test]
public void DefaultHttpClient()
{
10 changes: 10 additions & 0 deletions tests/UnitTestEx.NUnit.Test/ServiceBusFunctionTest.cs
Original file line number Diff line number Diff line change
@@ -151,5 +151,15 @@ public void Configuration_Overrride()
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
}

[Test]
public void Configuration_Overrride_Use()
{
// Demonstrates how to override the configuration settings for a test.
using var test = FunctionTester.Create<Startup>();
test.UseAdditionalConfiguration([new("SpecialKey", "NotSoSpecial")]);
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
}
}
}