Intercode.Toolbox.UnitTesting 2.5.0

dotnet add package Intercode.Toolbox.UnitTesting --version 2.5.0                
NuGet\Install-Package Intercode.Toolbox.UnitTesting -Version 2.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Intercode.Toolbox.UnitTesting" Version="2.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Intercode.Toolbox.UnitTesting --version 2.5.0                
#r "nuget: Intercode.Toolbox.UnitTesting, 2.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Intercode.Toolbox.UnitTesting as a Cake Addin
#addin nuget:?package=Intercode.Toolbox.UnitTesting&version=2.5.0

// Install Intercode.Toolbox.UnitTesting as a Cake Tool
#tool nuget:?package=Intercode.Toolbox.UnitTesting&version=2.5.0                

Intercode.Toolbox.UnitTesting

A trimmable, AOT-compatible .NET library that provides tools for unit testing.


DebugAssertListener

The DebugAssertListner hooks into the the TraceListner collection used by the Debug and Trace classes and listens for TraceListener.Fail calls. When a failure is triggered, the listener will throw a DebugAssertException with the message provided by the failure. This allows for unit tests to ensure that a particular assertion was triggered.

Usage

using var listener = new DebugAssertListener();
var act = () => Debug.Assert( false, "This is a test" );

act.Should()
    .Throw<DebugAssertException>();

listener.GetTrace()
        .Should()
        .Be( $"This is a test{Environment.NewLine}" );

Reference

Constructor

Create an instance of the DebugAssertListener class. This class implements the IDisposable interface and should be used in a using statement to ensure that the listener is removed from the Trace.Listeners collection when it is no longer needed.

DebugAssertListener()
Methods

Returns all the trace messages that have been captured by the listener.

string GetTrace()

DebugAssertException

The DebugAssertException is thrown by the DebugAssertListener when a failure is triggered by failures emitted by the Debug or Trace classes.

Constructors

Initializes a new instance of the DebugAssertException class with the specified error message

DebugAssertException( 
  string? message )

Initializes a new instance of the DebugAssertException class with the specified error and detail messages.

public DebugAssertException(
 string? message,
 string? detailMessage )  

Properties

Gets the detailed error message if it was provided; otherwise, it returns null.

string? DetailMessage { get; }

DependencyInjection

The DependencyInjection class provides foundational tools for unit testing classes that use the Microsoft.Extensions.DependencyInjection library; simplifying the process of creating and configuring a service provider for testing.

Usage

var values = new Dictionary<string, string?>
{
  { "MySection:Key1", "Value1" },
  { "MySection:Key2", "Value2" }
};

var configuration = DependencyInjection.CreateConfiguration( values );
var serviceProvider = DependencyInjection.CreateServiceProvider( 
  configuration,
  services => { services.AddSingleton( TimeProvider.System ); }
);

...

var timeProvider = serviceProvider.GetRequiredService<TimeProvider>();
var config = serviceProvider.GetRequiredService<IConfiguration>();
var section = config.GetSection( "MySection" );
var value = section["Key1"];

// Use configuration and timeProvider...

Reference

Methods
  • CreateConfiguration Creates a new IConfigurationRoot instance with optionally specified values by a Dictionary containing the configuration values and/or an Action that adds values to the configuration builder. If no value sare provided by the dictionary or the action, the configuration will be empty.
static IConfigurationRoot CreateConfiguration(
    Dictionary<string, string?>? values = null,
    Action<IConfigurationBuilder>? configureSource = null )
  • CreateServiceProvider creates a new IServiceProvider instance with the specified configuration and an optional action that allows for services to be added to the service collection. By default, the service collection will contain the configuration as a singleton instance.
static IServiceProvider CreateServiceProvider(
    IConfigurationRoot configurationRoot,
    Action<IServiceCollection>? servicesSetter = null )

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Intercode.Toolbox.UnitTesting:

Package Downloads
Intercode.Toolbox.UnitTesting.XUnit

Provides classes that enhance unit testing using the XUnit library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.0 80 11/13/2024
2.1.5 102 7/22/2024