I have an interface with a CopyFrom() method that copies all properties from another object. I have a test that performs several VerifyGet() calls to ensure that each property was retrieved from the passed object, e.g.:
Thing target = new Thing();
IThing source = new Mock<IThing>();
target.CopyFrom(source.Object);
source.VerifyGet(t => t.Foo);
source.VerifyGet(t => t.Bar);
I'd like a way to iterate over the properties of IThing
though and verify that each was copied automatically so the test will fail if someone adds a property but forgets to copy it. Is there a way to do this via Moq? I tried;
foreach (var prop in typeof(IThing).GetProperties())
{
source.VerifyGet(t => prop.Invoke(t, null));
}
but it didn't work since the lambda did not represent a property accessor. I think there should be a way to create something via the Expression
class, but I am not familiar enough with LINQ to figure out what should be in there.