Skip to content

A FluentAssertion extension to easily assert on callbacks.

Notifications You must be signed in to change notification settings

Phoenox/fluentassertions-callbacks

Repository files navigation

Fluentassertions.Callbacks

A FluentAssertion extension to easily assert on callbacks.

In our projects, we often take a more functional approach and inject callbacks. A lot. The test code often looked like this:

public void BadUnitTest
{
    // Arrange
    int? arg1;
    string? arg2;
    var testCallback = (int a1, string a2) =>
    {
        arg1 = a1;
        arg2 = a2;
    };
    var sut = new Sut();
    
    // Act
    sut.DoSomething(testCallback);
    
    // Assert
    arg1.Should().Be(42);
    arg2.Should().Be("Hello World");
}

To avoid the overhead of storing the arguments in local variables, I created this extension.

Installation

Add the FluentAssertions.Callbacks NuGet package to your test project.

Usage

using FluentAssertions.Callbacks;

public void GoodUnitTest()
{
    var testCallback = new CallbackSpy<int, string>();
    var sut = new Sut();
    
    sut.DoSomething(testCallback.Callback);
    
    testCallback.Should().HaveBeenCalledWith(42, "Hello World");
    
    // The following assertions are also possible
    testCallback.Should().HaveBeenCalled();
    testCallback.Should().HaveBeenCalledWith(Arg<int>.Is(x => x > 40), "Hello World");
}

About

A FluentAssertion extension to easily assert on callbacks.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages