The testing method WithCustomState doesn't work properly when the state is a string resulting from a concatenation.
See original GitHub issueHow to reproduce:
Add a validation that has a custom state as follow:
.WithState(x => "Test" + "123");
Add a unit test for this validation and try to assert it as follow:
.WithCustomState("Test123");
The test will fail with the message:
Message:
FluentValidation.TestHelper.ValidationTestException : Expected custom state of 'Test123'. Actual state was 'Test123'
I have got the same error using no matter what form of concatenation: StrigBuilder, String.Format, $ operator, + operator.
If you remove the concatenation from the validator and use it just as follow, the test works fine:
.WithState(x => "Test123");
The nuget version I used is the latest at this moment: 9.5.1
.
Could someone check that please? Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
The testing method WithCustomState doesn't work properly ...
The testing method WithCustomState doesn't work properly when the state is a string resulting from a concatenation. #1658.
Read more >How to fix my string concatenation that isn't working?
1 Answer 1 ... Syntax for string concatenation is incorrect. You are contacting string with object. {longitude} means {longitude : longitude}.
Read more >Custom State — FluentValidation documentation
The WithState method allows you to associate any custom data with the validation results. We could assign a custom state by modifying a...
Read more >Release Notes and History
Fixed Process activity not resetting the object's state when the object is preempted. ... Fixed Slot Label Painter not recognizing strings properly.
Read more >String Concatenation issue
Hi, We need to concatenate validation strings i.e. validation error messages from different parts of the rule/conditions and we are facing ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I see what’s happening. Internally a reference equality check is being performed. This example illustrates the problem:
Becuase the
==
operator is used against variables of typeobject
, the wrong type equality check is performed. Internally this is what FluentValidation is doing.When explicitly typed as strings, the result is as expected:
Internally FluentValidation needs to be changed to use
.Equals
, and also should optionally accept anIEqualityComparer
. To work around this for now, you can add your own test extension that takes a comparer:…which you can then use like this:
I’ll also ensure this is updated for a future version and I’ll post here once it’s done.
Implemented in 9.5.2