Skip to content

Configuring NFluent

Cyrille DUPUYDAUBY edited this page Jan 31, 2018 · 6 revisions

NFluent does not require any configuration effort

That being said, you can influence the behaviour through some properties.

EqualMode

You can change how equality is evaluated in most checks thanks to the Check.EqualMode property. Available optons are defined through the EqualityMode enumeration:

  • EqualityMode.FluentEqual: Comparison is done using Equals method, except for enumeration which are compared on a per entry basis, recursively (each comparison is made using FluentEqual logic). Notes:
    • _This algorithm may be adjusted in future version as it aims at providing the smoothest experience for IsEqualTo check.
    • This is the default equality algorithm since V2.1
    • Circular dependencies are properly handled_.
  • EqualityMode.Equals: comparison is made using using Equals method. Note:
    • This was the default mode for V2.0 and earlier
  • EqualityMode.OperatorEq: comparison is done using operator==() when available, revert to Equals otherwise.
  • EqualityMode.OperatorNeq: comparison is done using operator!=() when available, revert to Equals otherwise.

To change the current comparison logic, simply change the property to the desired value. E.g

  Check.EqualMode = EqualityMode.OperatorEq;
  // next check will use == if possible
  Check.That(sut).IsEqualTo(expected);
  // restore default mode
  Check.EqualMode = EqualityMode.FluentEqual;

You can alter the comparison logic at any moment, all subsequent checks will follow the new logic.

Maximum reporting length for values

You can specify the maximum string length for values in error messages using Check.StringTruncationLength. When error messages are generated, any value representation that is longer than this value will be truncated (in the middle of the string, using '...' as a marker). Example:

  Check.StringTruncationLength = 20;
  Check.That("Let's be honest, this a quite long texte").IsEqualTo("another long string, but different., otherwise it will succeed.");

will generate this error message

The checked string is different from expected one. At line 1, col 1, expected 'another long string,...' was 'Let's be honest, thi...'.
The checked string:
	["Let's be h...<<truncated>>...xte"]
The expected string:
	["another lo...<<truncated>>...ed."]

Clone this wiki locally