Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example usage of an async lambda.
Assert.Throws<SomeException>(async () => await obj.GetAsync());
@haacked

This comment has been minimized.

Copy link
Owner Author

haacked commented Jan 24, 2013

I saw some code that suggested to me that not everyone knows how you pass an await call as a lambda expression. It turns out the async keyword can apply to a lambda expression as well.

@nigel-sampson

This comment has been minimized.

Copy link

nigel-sampson commented Jan 24, 2013

Unless Assert.Throws takes a Func of Task instead of Action the compiler interprets the lambda as an async void method. This means it can't "await" the lambda and discover the exception.

@haacked

This comment has been minimized.

Copy link
Owner Author

haacked commented Jan 24, 2013

Thanks nigel! I saw the same thing here: http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx

@shiftkey

This comment has been minimized.

Copy link

shiftkey commented Jan 24, 2013

Somewhat related, I started hacking on a library to make testing Tasks better... https://github.com/pprovost/assertex

Goddamn it, its not on NuGet. I'll go stand in the corner until I've fixed that...

@haacked

This comment has been minimized.

Copy link
Owner Author

haacked commented Jan 24, 2013

@shiftkey

This comment has been minimized.

Copy link

shiftkey commented Jan 24, 2013

Just pushed AssertEx up to NuGet. Here's a teaser:

If you want to inspect the Task:

public class Subject
{
    public Task GetAsync()
    {
        return Task.Run(() => { throw new SomeException(); });
    }
}

...

AssertEx.TaskThrows<SomeException>(obj.GetAsync);
@shiftkey

This comment has been minimized.

Copy link

shiftkey commented Jan 24, 2013

I need to write up a decent wiki on how to test async code - all the horrors are coming back to me now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.