Skip to content

Configuring NFluent

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

NFluent does not require any configuration effort and works out of the box

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."]

Error details (V2.6)

NFluent focuses on providing only the most relevant information in case of check failure. Some checks can provide details information regarding the difference between the expected valued and the sut. For exmaple, string and enumeration comparison can provide you with a list of differences. By default, NFluent reports the first 5 differences. But you can control this thanks to the CountOfLineOfDetails property. As in:

// ensure up to 20 details are provided when comparing string or enumerations
Check.CountOfLineOfDetails = 10;

Customise error reporting

You can customise the error reporting mechanism, which default to raising the appropriate exception (depending on the unit test framework). You can provide your custom implementation of IErrorReporter and declare it as the default report using the Check.Reporter property:

  Check.Reporter = new MyReport();
Clone this wiki locally