4

Suppose I have an interface IPerson with 2 read properties age (int) and name (string).

I also have a class Person implementing IPerson.

How do I write a FsCheck generator for generating instances of IPerson type?

sthiers
  • 3,489
  • 5
  • 34
  • 47

1 Answers1

8

Something like the below should work:

Gen<IPerson> gen = from age in Arb.Default.Int32().Generator
                   from name in Arb.Default.String().Generator
                   select new Person(age, name) as IPerson;
Kurt Schelfthout
  • 8,880
  • 1
  • 30
  • 48
  • It works, indeed. But how can you use the "from" clause on Arb.Default.Int32().Generator, as IGen does not implement IEnumerable. What 's the magic? – sthiers Feb 09 '16 at 14:10
  • LINQ syntax works on any type that implements certain methods (Select, SelectMany, GroupBy etc), not just IEnumerable. Gen implements some of those. – Kurt Schelfthout Feb 17 '16 at 14:17