β Async void method testing in xUnit
In WinForms, it's required to use async void for event handlers like button clicks due to its synchronous nature. However, when trying to test an event handler like the one below, how can I indirectly "await" async void for proper method execution.
Async void method:
The test:
Any ideas on how to solve this?
15 Replies
Is just deleting this whole file an option?
I wish. Unfortunately there is alot of places in winforms where i will need to deal with async void since there is buttons everywhere that i need to test
What are you really testing?
in this particular instance, its just that the event is properly raised. If i remove await Task.Delay(100) from ValidFormAsync - it runs just fine but it hangs once i re-add it.
What if you don't test it at all?
I mean, i could but this feels like leaving the problem to the future. I assume there is atleast one case you would need to test async void? Or is that just a sign of bad code then?
What problem do you expect to have happen in the future?
As of now, none. Im just assuming there would be a situation somewhere and i havent seen it yet
I'm reading on my phone, but this seems like a bit of overkill
Could be, all good then. Ill look at how to re-design my code so the important parts are testable. Regardless, thanks for the assistance
Ideally the important parts aren't too tightly bound with winforms code
I feel like you're testing that when you press the button the button was pressed
It isnt what im testing but i get how it could look like that. So my codebase currently follows the MVP pattern. When Submit is clicked in the view, an event is invoked that is then subscribed to and handled by HandleSubmit_Clicked. I can just extract the code like this:
the async Task should be testable but it looks messy. If i try to make the handleSubmit itself async Task, i get the error "'Task DataFormPresenterTemplate.HandleSubmit_Clicked(object?, EventArgs)' has the wrong return type". Hence why i want to do a bit of research.
I should be good on my own though. Thank you once again for your input and hope you have a great day
Unknown Userβ’4w ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered
Thanks for the additional info. Will definitely look into itπ