-
Notifications
You must be signed in to change notification settings - Fork 53
Configuring NFluent
That being said, you can influence the behaviour through some properties.
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.
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."]
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;
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();
- Welcome
- How to start
- Customising error messages
-
Check.That
- All (Equality, Type)
- Reference types
- Members based
- IEnumerable (Properties, Content, Elements)
- String (Properties, Content, RegExp )
- Numeric Type(Properties, Comparisons, Floating)
- Dictionary
- Char (Properties, Value)
- IComparable
- DateTime
- DateTimeOffset
- Misc: Boolean, TimeSpan, Stream, Enum, EventWaitHandle
- Check.ThatCode
- Check.ThatDynamic
- Extensibility
- Auxiliary types (TimeUnit)
- Mocks