nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
423 stars 49 forks source link

'No parameterless constructor defined for type' error when using ruleset but same exception not thrown when using no ruleset #93

Open altmank opened 1 year ago

altmank commented 1 year ago

This might just be me not using it properly but having some issues using ruleset functionality. I get a MissingMethodException (No parameterless constructor defined for this object) in the below example when I expect it to function just the same as the generate call without the ruleset name but to use an age of 65 rather than the default of 18. It is intentional that Age is only accessible via a private setter and not exposed in the constructor.


void Main()
{
    var personFaker = new PersonFaker();    
    var defaultPerson = personFaker.Generate(); // No issues    
    var oldPerson = personFaker.Generate("OldAge"); //No parameterless constructor defined for this object exception.   
}

public sealed class PersonFaker : AutoFaker<Person>
{
    public PersonFaker()
    {
        RuleFor(x => x.Age, () => 18);
        RuleSet("OldAge", set =>
        {
            set.RuleFor(x => x.Age, () => 65);
        });
    }
}

public class Person
{
    public Person(string name)
    {
        Name = name;
    }
    public string Name { get; private set; }
    public int Age { get; private set; }
}