Testing ValueTask method with FluentAssertions, should not throw

147 Views Asked by At

I'm a bit confused here. I'm using FluentAssertions and I have this test method:

    [Test]
    public void DoNothingWhenEmpty()
    {
        FileCleanUpProcessor fileProcessor = new();
        Func<ValueTask> act = () => fileProcessor.ProcessCommands();
        act.Should().NotThrow();
    }

The MUT signature is the following:

public async ValueTask ProcessCommands() { ... }

While it works (I guess), I wonder if I've written this correctly. And why method NotThrowAsync() is not available/not implemented for ValueTask.

What am I missing?

1

There are 1 best solutions below

1
Nkosi On

While it works (I guess), I wonder if I've written this correctly

Given that the subject under test is asynchronous then it stands to reason that the test should be async as well.

[Test]
public async Task DoNothingWhenEmpty() {
    //Arrange
    FileCleanUpProcessor fileProcessor = new();

    //Act
    Func<Task> act = async () => await fileProcessor.ProcessCommands();

    //Assert
    await act.Should().NotThrowAsync();
}

And why method NotThrowAsync() is not available/not implemented for ValueTask

NotThrowAsync() was defined for either Task or Task<TResult> and as such is not available for ValueTask

Reference FluentAssertions : Exceptions