ToolBX.Eloquentest 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ToolBX.Eloquentest --version 1.1.0
NuGet\Install-Package ToolBX.Eloquentest -Version 1.1.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="ToolBX.Eloquentest" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ToolBX.Eloquentest --version 1.1.0
#r "nuget: ToolBX.Eloquentest, 1.1.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 ToolBX.Eloquentest as a Cake Addin
#addin nuget:?package=ToolBX.Eloquentest&version=1.1.0

// Install ToolBX.Eloquentest as a Cake Tool
#tool nuget:?package=ToolBX.Eloquentest&version=1.1.0

Eloquentest

Eloquentest

A simple to use .NET unit testing framework built on top of MSTest and Moq. It also includes built-in support for services that are injected using [AutoInject].

Getting started

Here is the dumbest service you can imagine.

    public class DumbestServiceYouCanImagine
    {
        public string Format(int number)
        {
            return number == 0 ? "It's zero" : "It's not zero";
        }
    }

Here is the dumbest unit test for the dumbest service you can imagine.

    [TestClass]
    public class DumbestServiceYouCanImagineTester : Tester<DumbestServiceYouCanImagine>
    {
        [TestMethod]
        public void WhenNumberIsZero_ReturnItsZero()
        {
            //Act
            var result = Instance.Format(0);

            //Assert
            Assert.AreEqual("It's zero", result);
        }
    }

That's all well and good but what if your dumb service had dependencies to other services though?

public interface ISomeOtherService
{
    public string UserId { get; }
    public IReadOnlyList<int> Roles { get; }
}
public class DumbestServiceYouCanImagine
{
    private readonly ISomeOtherService _someOtherService;

    public DumbestServiceYouCanImagine(ISomeOtherService someOtherService)
    {
        _someOtherService = someOtherService;
    }

    public string DoSomeOtherStuff()
    {
        return _someOtherService.Roles.Contains(8) ? 
            $"User {_someOtherService.UserId} is authorized to do dumb stuff." : 
            $"User {_someOtherService.UserId} is strictly forbidden from doing dumb stuff!";
    }
}
[TestClass]
public class DumbestServiceYouCanImagineTester : Tester<DumbestServiceYouCanImagine>
{
    [TestMethod]
    public void WhenContainsRoleNumberEight_SayThatUserIsAuthorized()
    {
        //Arrange
        var userId = Fixture.Create<string>();
        GetMock<ISomeOtherService>().Setup(x => x.UserId).Returns(userId);
        
        GetMock<ISomeOtherService>().Setup(x => x.Roles).Returns(new List<int> { 8 });

        //Act
        var result = Instance.DoSomeOtherStuff();

        //Assert
        Assert.AreEqual($"User {userId} is authorized to do dumb stuff.", result);
    }

    [TestMethod]
    public void WhenDoesNotContainRoleNumberEight_SayThatUserIsUnauthorized()
    {
        //Arrange
        var userId = Fixture.Create<string>();
        GetMock<ISomeOtherService>().Setup(x => x.UserId).Returns(userId);

        GetMock<ISomeOtherService>().Setup(x => x.Roles).Returns(new List<int>());

        //Act
        var result = Instance.DoSomeOtherStuff();

        //Assert
        Assert.AreEqual($"User {userId} is strictly forbidden from doing dumb stuff!", result);
    }
}

Testing collections

Ever wanted to test whichever item from a collection?

[TestMethod]
public void WhenYouWantWhichever_GetWhichever()
{
    //Arrange
    var list = Fixture.CreateMany<string>().ToList();

    var something = Fixture.Create<Something>();
    GetMock<ISomeShadyService>().Setup(x => x.GetSomething(list.GetRandom())).Returns(something);

    //Act
    var result = Instance.DoSomething(list);

    //Assert
    result.Should.BeEquivalentTo(something);
}

You can also do the same thing but with the index only if that's what floats your boat.

var index = list.GetRandomIndex();

Using the generic tester with custom constructor parameters

It�s all well and good but what if you want to use the generic Tester class while providing your own parameters to the tested class� constructor instead of the automatically generated mocks and fixtures?

You can do that with the ConstructWith() method.

[AutoInject]
public class GameObjectFactory : IGameObjectFactory
{
    private readonly IServiceProvider _serviceProvider;

    public GameObjectFactory(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public IGameObject Create(GameObjectConfiguration configuration) => new GameObject(_serviceProvider, configuration);
}

public record GameObjectConfiguration(string Name, Vector2<float> InitialPosition, Direction InitialDirection);

//The service provider is passed by the GameObjectFactory and is always the same reference
//Configuration is unique to every instance so we need to be able to override it if we want to test that things are correctly initialized
public GameObject(IServiceProvider serviceProvider, GameObjectConfiguration configuration)
{
    _game = serviceProvider.GetRequiredService<IGame>();
    Name = configuration.Name;
    Position = configuration.InitialPosition;
    Direction = configuration.InitialDirection;
}

[TestClass]
public class GameObjectTester 
{
    [TestClass]
    public class Move : Tester<GameObject>
    {
        [TestMethod]
        public void Always_ChangePosition()
        {
            //Arrange
            var configuration = Fixture.Create<GameObjectConfiguration>();

            //It is important to call ConstructWith before accessing the Instance property!
            ConstructWith(configuration);

            //Act
            Instance.Move(new Vector2<float>(1, 0));

            //Assert
            Instance.Position.Should().Be(configuration.InitialPosition + new Vector2<float>(1, 0));
        }
    }
}

Integration tests

The Eloquentest.Integration namespace (available on nuget.org as a separate package) provides tools to leverage MSTest to execute code without mocking all while using the Eloquentest structure and syntax you may already be familiar with.

IntegrationTester and IntegreationTester<T> replace Tester and Tester<T> and there are no mocks.

Setup

It works right out of the box if you already use AutoInject in your regular code.

Breaking changes

1.0.X → 1.1.X GetRandom and GetRandomIndex methods have been removed from Eloquentest. Please import and use ToolBX.OPEX from nuget.org instead.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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 ToolBX.Eloquentest:

Package Downloads
ToolBX.Collections.UnitTesting

Tester classes and extensions to help with unit testing ToolBX.Collections.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.1 2,440 1/17/2024
2.2.1-beta.1 48 1/14/2024
2.2.0 208 1/11/2024
2.2.0-beta97 117 1/7/2024
2.2.0-beta96 57 1/4/2024
2.2.0-beta94 74 12/29/2023
2.2.0-beta93 87 12/29/2023
2.2.0-beta92 71 12/29/2023
2.2.0-beta91 73 12/29/2023
2.2.0-beta9 101 12/14/2023
2.2.0-beta8 64 12/13/2023
2.2.0-beta7 109 11/24/2023
2.2.0-beta6 64 11/22/2023
2.2.0-beta5 61 11/22/2023
2.2.0-beta4 75 11/17/2023
2.2.0-beta3 70 11/17/2023
2.2.0-beta2 54 11/17/2023
2.2.0-beta11 80 12/27/2023
2.2.0-beta10 81 12/14/2023
2.2.0-beta1 67 11/17/2023
2.2.0-beta.12 62 12/28/2023
2.2.0-alpha.1 63 12/28/2023
2.1.4 2,729 11/22/2023
2.1.4-beta4 64 11/22/2023
2.1.4-beta3 70 11/17/2023
2.1.4-beta2 92 11/2/2023
2.1.4-beta1 75 10/28/2023
2.1.3 2,550 10/17/2023
2.1.3-beta1 77 10/17/2023
2.1.2 523 10/4/2023
2.1.2-beta7 112 10/3/2023
2.1.2-beta6 682 8/29/2023
2.1.2-beta5 80 8/28/2023
2.1.2-beta4 80 8/25/2023
2.1.2-beta3 88 8/24/2023
2.1.2-beta2 1,834 6/23/2023
2.1.2-beta1 105 6/22/2023
2.1.1 284 6/19/2023
2.1.0 226 6/13/2023
2.1.0-beta3 125 6/13/2023
2.1.0-beta2 106 6/12/2023
2.1.0-beta1 113 6/10/2023
2.0.2 942 4/25/2023
2.0.1 170 4/23/2023
2.0.0 374 11/9/2022
2.0.0-beta2 126 9/28/2022
2.0.0-beta1 119 9/20/2022
1.1.2 382 9/28/2022
1.1.1 384 9/20/2022
1.1.1-beta3 143 8/10/2022
1.1.1-beta2 119 8/10/2022
1.1.1-beta1 119 8/10/2022
1.1.0 370 8/10/2022
1.0.7 366 5/27/2022
1.0.6 385 5/16/2022
1.0.5 422 3/15/2022
1.0.5-beta 136 3/15/2022
1.0.4 435 2/5/2022
1.0.3 398 2/4/2022
1.0.2 392 1/14/2022
1.0.1 233 12/22/2021
1.0.0 265 12/15/2021