soenneker.utils.autobogus
soenneker.utils.autobogus copied to clipboard
Private readonly fields - are overwritten/generated automatically
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);
}
Br