Search code examples
c#fluent-assertions

Customize failure handling in FluentAssertions


I'm trying to use FluentAssertions not only as testing assertions framework but also to check runtime contracts (like advanced Debug.Assert, and I have read this question). The desired behaviour is:

  • In debug mode: throw exceptions (it does).
  • In release mode: do not stop method execution (no exception is thrown) but execute custom action (write to log).

Is there a way to customize failure handling behaviour? There is IAssertionStrategy interface which is exactly about failure handling. However, it's implementations are hard-coded inside the AssertionScope. Did I miss something in the sources?


Solution

  • You can set FluentAssertions.Common.Services.ThrowException to override the behavior. Note though it was never designed for that.