{"id":5294,"date":"2017-09-06T19:58:59","date_gmt":"2017-09-06T19:58:59","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5294"},"modified":"2017-09-06T19:58:59","modified_gmt":"2017-09-06T19:58:59","slug":"beware-those-async-void-exceptions-in-unit-tests","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/beware-those-async-void-exceptions-in-unit-tests\/","title":{"rendered":"Beware those async void exceptions in unit tests"},"content":{"rendered":"<p>This is a cautionary tale&#8230;<\/p>\n<p>I&#8217;ve been moving my builds to a new server box and in doing so started to notice some problems. These problems didn&#8217;t exist on the previous build box and this might be down to the updates versions of the tools, such as nant etc. I was using on the new box.<\/p>\n<p>Anyway the crux of the problem was I was getting exceptions when the tests were run, nothing told me what assembly or what test, I just ended up with<\/p>\n<p>NullReference exceptions and a stack trace stating this <\/p>\n<p>System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(System.Object)<\/p>\n<p>Straight away it looks like async\/await issues. <\/p>\n<p>To cut a long story short.<\/p>\n<p>I removed all the Test assemblies, then one by one place them back into the build, running the &#8220;test&#8221; target in nant each time until I found the assembly with the problems. Running the tests this way also seemed to report more specifics about the area of the exception.<\/p>\n<p>As we know, we need to be particularly careful handling exceptions that occur within an async void method (mine exist because they&#8217;re event handlers which are raised upon property changes in a view model).<\/p>\n<p>Interestingly when I ran the Resharper Test Runner all my tests passed, but switching to running in the tests in Debug showed up all the exceptions (ofcourse assuming that you have the relevant &#8220;Exceptions&#8221; enabled within Visual Studio). The downside of this approach, ofcourse, is that I also see all the exceptions that are intentionally tested for, i.e. ensuring my code exceptions when it should. But it was worth it to solve these problems. <\/p>\n<p>Luckily none of these exceptions would have been a problem in normal running of my application, they were generally down to missing mocking code or the likes, but it did go to show I could be smug in believing my tests caught every issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a cautionary tale&#8230; I&#8217;ve been moving my builds to a new server box and in doing so started to notice some problems. These problems didn&#8217;t exist on the previous build box and this might be down to the updates versions of the tools, such as nant etc. I was using on the new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[93,28],"tags":[],"class_list":["post-5294","post","type-post","status-publish","format-standard","hentry","category-asynchronous","category-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5294"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5294\/revisions"}],"predecessor-version":[{"id":5353,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5294\/revisions\/5353"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}