soenneker.utils.autobogus icon indicating copy to clipboard operation
soenneker.utils.autobogus copied to clipboard

Private readonly fields - are overwritten/generated automatically

Open rizi opened this issue 1 month ago • 2 comments

Hi,

With this fix: #143, private readonly fields are now also set automatically, is there a way to prevent/controll this with some kind of configuration (per type)?

This can cause issues when the field is initilized via constructor, because afterwards it's getting overwritten bei Autobogus.

Here is a very simple sample.

public class AnotherObjectToFake
{
    private readonly string _key;

    public AnotherObjectToFake(string key)
    {
        _key = key;
    }

    public string GetKey()
    {
        return _key;
    }
}

//test class
 [TestMethod]
 public void PrivateReadOnlyField_Should_Not_Be_Overwritten()
 {
     //arrange
     const string key = "someKey";

     //act
     AnotherObjectToFake objectToFake = new AutoFaker<AnotherObjectToFake>()
         .CustomInstantiator(_ => new AnotherObjectToFake(key));

     //assert
     objectToFake.GetKey().Should().Be(key);
 } 

image

Br

rizi avatar May 24 '24 08:05 rizi