NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

Fixed bug that would cause a struct to not match and throw a runtime error.

Open Notallthatevil opened this issue 6 months ago • 9 comments

When a struct had an empty constructor that modified a field/property to a non-default value. The matcher would fail and throw a runtime error. This bug fix attempts to fix that issue by essentially zero initializing the struct the same way that default(T) does through the use of a runtime helper method. Simply, we replace Activator.CreateInstance with GetUninitializedObject.

The idea came from this post

Which contains and deprecated method as of .NET8. Microsoft has a recommended fix for that here.

This resolves the issue in #766

Notallthatevil avatar Aug 15 '25 16:08 Notallthatevil

Thanks for the pull request!

Could you please add some unit tests?

304NotModified avatar Aug 15 '25 16:08 304NotModified

I added a simple test that does fail on the main branch. Not sure there is a whole lot to test here. I did ensure that all tests pass, especially ones that take in nullable primitive types like int? as they need special handling with the new DefaultInstanceOfValueType functionality. If you have any specific scenarios you would like to see let me know.

Notallthatevil avatar Aug 15 '25 17:08 Notallthatevil

Is there anything still blocking this from being merged?

Notallthatevil avatar Sep 04 '25 14:09 Notallthatevil

I fixed the format issue.

Notallthatevil avatar Sep 04 '25 15:09 Notallthatevil

Is there anything still blocking this from being merged?

Only a review I guess 😅

304NotModified avatar Sep 04 '25 15:09 304NotModified

@304NotModified Anything still holding this back that I can help with? I would love to not have to maintain my own branch anymore and it seems that others have faced problems similar to this one.

Notallthatevil avatar Oct 07 '25 14:10 Notallthatevil

cc @304NotModified

Romfos avatar Nov 13 '25 21:11 Romfos

@304NotModified I am sure you are busy, is there anything I can help with that is holding you back from approving these changes?

Notallthatevil avatar Dec 08 '25 18:12 Notallthatevil

Hey!

I'm not that active anymore on NSubstitute 😅 And I got hunderds of notifications/tags recently, so dropped a lot of those notifications 😅

I could try to check this PR this week, but I'm not sure if this needs an approval of me?

304NotModified avatar Dec 08 '25 23:12 304NotModified

reviewed. There is still an open discussion in this PR. I don't know the exact side effects of this change, so unfortunately I cannot approve this.

@dtchepak maybe enable the check of the discussions so we could not forget this

Under settings -> rules -> rulesets

image

304NotModified avatar Dec 12 '25 22:12 304NotModified