I am attempting to initialize my model entities using FsCheck. The models live in C# and are normally initialized via Entity Framework via their private setters. For example (contrived):
public class Model
{
public string One { get; private set; }
public int Two { get; private set; }
}
I would like to create an FsCheck generator that automatically uses the registered generator for each property to produce the model. Something like this:
let modelGenerator =
gen {
let incident = new Model()
typeof<Model>.GetProperties()
|> Array.filter (fun p -> p.CanWrite)
|> Array.iter (fun p ->
let! newVal = Arb.generateType p.PropertyType // I wish I could do this
p.SetValue(incident, newVal))
return incident
}
There are two things wrong with this:
- The
let!
can't be used outside of thegen
computation expression. Arb.generateType
doesn't exist, and I can't find a way to do its equivalent
Is it possible to create a generator that will automatically set the private fields on my model?