question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AssertionExtensions.Awaiting Task method is ambiguous with ValueTask

See original GitHub issue

With .net core and c# 8 how are you supposed to use the AssertionExtensions.Awaiting methods? The following code worked before ValueTask was added, but now will give the following compile time error

The call is ambiguous between the following methods or properties: ‘AssertionExtensions.Awaiting<T>(T, Func<T, Task>)’ and ‘AssertionExtensions.Awaiting<T>(T, Func<T, ValueTask>)’

[Test]
public async Task TestFoo()
{
    var foo = new Foo();

    foo.Awaiting(async f => await f.BarAsync()).Should().NotThrow(); //<--compiler error
}

public class Foo
{
    public Task BarAsync() => Task.FromResult(0);
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
CraigRicecommented, Aug 21, 2023

Some related information:

I had a similar issue when having to force LangVersion 6 on an old project (to use c# 6), the Should() assertion extensions wouldn’t compile with “The call is ambiguous between the following methods or properties: ‘DataRowAssertionExtensions.Should<TDataRow>(TDataRow)’ and ‘DataSetAssertionExtensions.Should<TDataSet>(TDataSet)’”.

The workaround is changing “using FluentAssertions;” to “using static FluentAssertions.AssertionExtensions;”

1reaction
markusschabercommented, Oct 5, 2020

The example here still advises async + await: https://fluentassertions.com/exceptions/

Alternatively, you can use the Awaiting method like this:

Func<Task> act = () => asyncObject.Awaiting(async x => await x.ThrowAsync<ArgumentException>()); act.Should().Throw<ArgumentException>();

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing for exceptions in async methods
Internally this method will run task returned by Func and wait for it. ... //Act and Assert Func<Task> sutMethod = async () =>...
Read more >
ValueTask<TResult> Struct (System.Threading.Tasks)
A ValueTask<TResult> instance may only be awaited once, and consumers may not read Result until the instance has completed. If these limitations are ......
Read more >
Async block compiler error "Type of expression is ...
I'm trying to make an interactor struct that will call an API method to retrieve a card, but the compiler is throwing "Type...
Read more >
Understanding Control Flow with Async and Await in C# | ...
You might recall from the previous guide that the async keyword is actually just a way to eliminate ambiguity for the compiler with...
Read more >
Understanding how to use Task and ValueTask - YouTube
[14:42] - What can you do with Task vs ValueTask ? ... C# Async / Await - Make your app more responsive and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found