Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

240 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EvilBaschdi.Testing

Source Code of EvilBaschdi.Testing

License: MIT

Package Feeds

Default by NuGet.config is myget.org

Feed Feed Url
myget.org https://www.myget.org/F/evilbaschdi/api/v3/index.json
codeberg.org https://codeberg.org/api/packages/evilbaschdi/nuget/index.json

Quality & Activity

Branch Status & Activity
Main Branch CodeFactor Commit Activity Main Last Commit Main
Develop Branch CodeFactor Commit Activity Develop Last Commit Develop

Packages

Branch Version
Main Branch MyGet Version
Develop Branch MyGet Version
Feed Package Url
myget.org https://myget.org/feed/evilbaschdi/package/nuget/EvilBaschdi.Testing
codeberg.org https://codeberg.org/evilbaschdi/-/packages/nuget/EvilBaschdi.Testing

Installation

PM> Install-Package EvilBaschdi.Testing

Features

This library provides a comprehensive set of testing utilities and assertion extensions:

  • Guard Clause Assertions - Verify null guards on async methods
  • Fluent Assertions for Microsoft.Extensions.DependencyInjection - Assert service configurations
  • FluentAssertions Extensions - Enhanced testing for dependency injection

AutoFixture Attributes

NSubstituteOmitAutoPropertiesTrueAutoDataAttribute

A custom xUnit [Theory] attribute that combines AutoFixture's AutoDataAttribute with NSubstitute automatic mocking and sets OmitAutoProperties = true. This means the fixture will create mocks but will not automatically populate properties—you must explicitly configure them.

using EvilBaschdi.Testing;
using Xunit;

[Theory, NSubstituteOmitAutoPropertiesTrueAutoData]
public void MyTest(IMyService service, MyDependency dependency)
{
    // service and dependency are automatically created and injected
    // properties are not auto-populated - configure them as needed
}

NSubstituteOmitAutoPropertiesTrueInlineAutoDataAttribute

Combines InlineAutoDataAttribute with NSubstitute automatic mocking and OmitAutoProperties = true. Allows you to provide inline data values alongside auto-generated dependencies.

using EvilBaschdi.Testing;
using Xunit;

[Theory]
[NSubstituteOmitAutoPropertiesTrueInlineAutoData("value1", 42)]
public void MyTest(string inlineValue, int inlineNumber, IMyService service)
{
    // inlineValue and inlineNumber come from the attribute parameters
    // service is automatically created by the fixture
}

Guard Clause Assertions

Overview

GuardClauseAssertionExtensions provides extension methods for verifying that all public asynchronous methods on a specified type have appropriate null guards for their reference type parameters.

using EvilBaschdi.Testing.Extensions;
using AutoFixture.Xunit3;

[Theory, NSubstituteOmitAutoPropertiesTrueAutoData]
public void VerifyAllAsyncMethodsHaveNullGuards(GuardClauseAssertion assertion)
{
    assertion.VerifyTask<MyAsyncService>(
        typeof(MyAsyncService).GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance));
}

VerifyTask

Verifies that all public async methods on type T have null guards for their reference type parameters.

var assertion = new GuardClauseAssertion(new Fixture());
assertion.VerifyTask<MyService>(methodInfos);
  • Parameters:
    • assertion - The GuardClauseAssertion instance
    • methodInfos - Collection of methods to verify
  • Throws: GuardClauseException if a method doesn't have proper null guards
  • Note: Value types and non-reference parameters are automatically skipped

Fluent Assertions for Microsoft.Extensions.DependencyInjection

This library contains fluent assertion extensions for testing Microsoft.Extensions.DependencyInjection service configurations.

Note: This library is based on FluentAssertions.Microsoft.Extensions.DependencyInjection by zachdean.

Quick Start

using EvilBaschdi.Testing;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddSingleton<ISomeService, SomeService>();
services.AddTransient<ITransient, Transient>();
services.AddScoped<IScoped, Scoped>();

// Assert that a service is registered with the correct implementation and lifetime
services.Should()
    .HaveService<ISomeService>()
    .WithImplementation<SomeService>()
    .AsSingleton();

API Reference

HaveCount(int expected)

Asserts that the service collection contains the expected number of service registrations.

services.Should().HaveCount(5);

HaveService<TService>()

Asserts that a service of type TService is registered in the service collection.

services.Should().HaveService<ISomeService>();

WithCount(int expected)

Asserts that the service is registered a specific number of times. Can be chained before lifetime assertions.

services.Should()
    .HaveService<ISomeService>()
    .WithCount(2);

WithImplementation<TImplementation>()

Asserts that the service is registered with a specific implementation type. Can be chained for multiple implementations.

services.Should()
    .HaveService<ISomeService>()
    .WithImplementation<SomeService>();

WithFactory()

Asserts that the service is registered with a factory function (e.g., via AddSingleton(provider => ...), AddScoped(provider => ...), AddTransient(provider => ...)).

services.Should()
    .HaveService<ISomeService>()
    .WithFactory();

WithFactory(Func<IServiceProvider, TService> expectedFactory)

Asserts that the service is registered with a specific factory function.

services.Should()
    .HaveService<ISomeService>()
    .WithFactory(provider => new SomeService());

Lifetime Assertions

  • AsSingleton() - Asserts that the service is registered as a singleton
  • AsScoped() - Asserts that the service is registered as scoped
  • AsTransient() - Asserts that the service is registered as transient
services.Should()
    .HaveService<ISomeService>()
    .AsSingleton();

services.Should()
    .HaveService<IRepository>()
    .AsTransient();

services.Should()
    .HaveService<IUnitOfWork>()
    .AsScoped();

Method Chaining with And()

Chain multiple assertions together:

services.Should()
    .HaveService<ISomeService>()
    .AsSingleton()
    .And()
    .HaveCount(3);

About

xunit test extensions

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages