FluentAssertions ShouldNotThrow is not recognised for an async method/Func
Asked Answered
A

2

13

I am trying to check an async method throws concrete exception.

For that I am using MSTEST and FluentAssertions 2.0.1.

I have checked this Discussion on Codeplex and to see how it works with async-exception methods this another one link about FluentAssertions async tests:

After a while trying to work with my 'production' code I have switched off to the Fluentassertions fake aync class and my resulting code is like this (put this code inside a [TestClass]:

[TestMethod]
public void TestThrowFromAsyncMethod()
{
    var asyncObject = new AsyncClass();
    Action action = () =>
    {
        Func<Task> asyncFunction = async () =>
        {
            await asyncObject.ThrowAsync<ArgumentException>();
        };
        asyncFunction.ShouldNotThrow();
    };
}


internal class AsyncClass
{
    public async Task ThrowAsync<TException>()
        where TException : Exception, new()
    {
        await Task.Factory.StartNew(() =>
        {
            throw new TException();
        });
    }

    public async Task SucceedAsync()
    {
        await Task.FromResult(0);
    }
}

The problem is that ShouldNotThrow is not valid:

ShouldNotThrow method is not recognised by the code. If I try to compile, it gives me this error: 'System.Func' does not contain a definition for 'ShouldNotThrow' and the best extension method overload 'FluentAssertions.AssertionExtensions.ShouldNotThrow(System.Action, string, params object[])' has some invalid arguments

Thanks.


SOLUTION

2.0.1 FA version doesn't support this ShouldNotThrow functionality and it will be included in the next reléase 2.1 (near next week).

Note: ShouldThrow is already supported in 2.0.1 versión.

Anent answered 14/8, 2013 at 19:11 Comment(5)
I don't know anything about FluentAssertions, but the exception says that ShouldNotThrow is only defined for Action and not Func.Mirabella
@KeithPayne Which means that FluentAsseritions probably doesn't support async.Fanjet
FluentAssertions supports async, if you read the link above (link), you can see it. And this one could help too: fluentassertions.codeplex.com/workitem/12148Anent
@ferpega: Are you targeting .NET 4.5 in your unit test project? If so, try uninstalling FA and reinstalling it.Turfy
@StephenCleary: Target is .NET 4.5 FluentAssertions is Nuget managed and runtime-version-property is: v4.0.30319Anent
O
18

You don't need the encompassing Action. That is only used in the unit tests to verify that the API is throwing the right exception. This should be sufficient:

[TestMethod]
public void TestThrowFromAsyncMethod()
{
    Func<Task> asyncFunction = async () =>
    {
        await asyncObject.ThrowAsync<ArgumentException>();
    };

    asyncFunction.ShouldNotThrow();
}

Unfortunately the ShoudlNotThrow() on a Func is missing from .NET 4.5. I've fixed this in release 2.1 (currently dogfooding).

Oliana answered 15/8, 2013 at 6:9 Comment(5)
When posting code, please highlight it and hit the {} button (or manually indent each line by four spaces if you need the exercise).Pumpernickel
@DennisDoomen I know, I was only writting the same exact code what is passing tests in FA. But if my framework already is 4.5 why does FA install 4.0.... with nuget ?Anent
@DennisDoomen I mean, FA is Runtime version 4.0.30319 and it is already located on net45 folder with this complete path: ....\packages\FluentAssertions.2.0.1\lib\net45\FluentAssertions.dll. But No ShouldNotThrow is available.Anent
Hmm. I need to try that. 4.0.30319 = .NET 4.5Oliana
It would be nice if this method was exposed as "async" so it could be awaited. Both VS and Resharper complain about not having await/async implemented correctly. See MS's Assert.ThrowsExceptionAsync()Thorn
F
1

If you look at AssertionExtensions.cs class you will see that the ShouldNotThrow extension method on Func is only defined for net45 or winrt compilation targets.

Check this:

  1. Your unit tests project is on .net 4.5 or winrt
  2. The referenced assertion library is the .net 4.5 one, if not try changing the referenced FluentAssertions library to the right one.

Also after doing this, I think you need to call the action method to do the assertion, otherwise the inner lambda will not be called:

[TestMethod]
public void TestThrowFromAsyncMethod()
{
    var asyncObject = new AsyncClass();
    Action action = () =>
    {
        Func<Task> asyncFunction = async () =>
        {
            await asyncObject.ThrowAsync<ArgumentException>();
        };
        asyncFunction.ShouldNotThrow();
    };

    action.ShouldNotThrow();
}
Fcc answered 14/8, 2013 at 22:51 Comment(5)
Target is 4.5 but the runtime versión is: v4.0.30319. FluentAssertions is Nuget Managed.Anent
Please check that the referenced FluentAssertions library comes from the right folder named "net45" inside the nuget package folder.Scholasticate
In the soon to be released FA 2.1, those extension methods will be supported for .NET 4.0Oliana
@DennisDoomen I'm impatient. :-)Anent
@iCe: Yes, FluentAssertions library is in the net45 folder.Anent

© 2022 - 2024 — McMap. All rights reserved.