Leave it to me to try the most complex solution first, and then later realize there was a far easier way to do it! OK, so I’m trying to develop a higher-quality version of an OWIN provider to work with ASP.NET, for use in new projects, going forward. Just about all of OWIN uses the notorious async/await pattern.
What I ended up with was something like this, for a unit test:
It looks like it should work, and voila – my tests run green. Wait a minute, all my tests run green – even ones that should fail!
It turns out that the Assert statements do get run, but not in the context of the unit test. Presumably, they are executed in another thread, so the unit test never catches the assertion!
After a little investigating, it turns out I was overthinking this.
MSTest from VS2012 onward supports async/await. The only catch is the the unit test can’t have a void return type, it must return a task.
So, if I refactor the test into something like this:
and ironically, I found a bug in the unit test, the second assert should have been IsFalse(..). Now that can async/await and actually get assertions, I now have successful, and simplified unit tests!