Search code examples
unit-testingasynchronousnunitresharperfluent-assertions

Prevent Unit Tests against Task<t>


I have the problem that some Unit Tests are written against async methods. These methods return Task<t> and I found one that tests if the return value is not null. Instead the test should have tested if the return value of the task is not null. This is also a problem when changing a method from sync returning t to async returning Task<t>. Are there any ways to show a warning when a Unit Test tests against Task? I'm using NUnit, FluentAssertions and ReSharper. Preferred would of course be something that resides within the code base and can be shared with the rest of the team (like ReSharper settings).


Solution

  • I finally ended up with ReSharper Custom Patterns.

    $task$.Should()
    

    with task being an Expression placeholder with Expression Type = System.Threading.Tasks.Task. Then I set the severity to Warning.