I donβt think that this is possible with Moq, but first of all you need to ask a question about whether your test is relevant. What do you really want to check here?
Is it important for the CopyFrom method to read any properties? He can definitely read all the properties without writing them to a new instance, so such an interaction-based test really proves nothing.
I assume that you really would like to verify that the properties of the target are equal to the properties of the source?
Assuming that the properties on IThing are writable, you can create a Stub with all the properties set using the SetupAllProperties method:
var sourceStub = new Mock<IThing>(); sourceStub.SetupAllProperties(); sourceStub.Object.Bar = "Bar"; sourceStub.Object.Foo = "Foo";
Then you need to compare the target with the source to see if all the properties match. This can be done by implementing the Equal Test Equals method in a class that wraps the real target.
If you think this is too much work, you can check the AutoFixture Likeness Class, which gives you universal equality testing. This will allow you to continue the test as follows:
var expectedResult = new Likeness<IThing>(sourceStub.Object); target.CopyFrom(sourceStub.Object); Assert.AreEqual(expectedResult, target);
Likeness uses Reflection to simply iterate over all the public properties in the wrapped object and see if the compared object has the same values ββfor these properties.
source share