chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes

I am using Isolator v7.0.9.

I would like to know how to fake a static field that is readonly and initialized inline in a static class. I'd prefer not to have to change the field to a property with only a getter just to be able to test the field - bad practice to change your code for unit tests :D

If there isn't a way to do this because it is initialized inline, can I fake it if I move the initialization into a static ctor? Again, not preferable, but...

Thank you for your time.
asked by dblack (8.4k points)

1 Answer

–1 vote

You can easily change a private field value using ObjectState.

For example:
ObjectState.SetField(typeof(MyType), "myCounter", 42)
answered by Elisha (12k points)
While this does work, if the static field was readonly and you change the value in a second test, then the second test does not observe the new value in its test run.