FluentAssertions – Make null and empty strings be equivalent

FluentAssertions is my assertion library of choice when writing tests in C#. It is very easy to extend and has a ton of nice functionality out of the box.

Unfortunately, there seems to be no way to make a string be equivalent if it is both null and empty. Thankfully we can create our own using AssertionOptions.AssertEquivalencyUsing!

AssertionOptions.AssertEquivalencyUsing(options =>
  options.Using<string>(ctx => (ctx.Subject ?? string.Empty).Should().BeEquivalentTo(ctx.Expectation ?? string.Empty)).WhenTypeIs<string>()
);
Did this post help you? Buy me a coffee!
Buy Me A Coffee
comments powered by Disqus