Guido van Loon
Entries from November 2007
Calendar
| November '07 | ||||||
| Mo | Tu | We | Th | Fr | Sa | Su |
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | ||
Tag Cloud
Archives
Technorati
I needed an easy way to verify if the system under test (sut) was calling the correct methods with the correct parameters as recorded. Rhino Mocks does this by default but my parameters are objects, or even small object trees. Rhino Mocks will compare the pointers of the expected and actual objects instead of comparing the contents of both objects.
I know this can be done with the Property constraint but it works string based and when the object has a lot of properties you must program as many Property constraints as there are poperties.
To overcome this cumbersome task I've created the AllPropertiesMatchConstraint class which can be used through a ReferenceType helper. The syntax looks like this:
Now when HasInventory is called with a different product instance who has the same property values the expectation will still be met. However, if HasInventory is called with a different product who has other property values the constraint will tell you execly which property is different and therefore does not match your expectation.
The way this works it with a pinch of reflection
Through reflection all public properties and public fields of the expected object are checked with the properties and fields of the actual object. This works recursive so if a property, in it's self, is an object, that object will also be matched.
Collections are supported through the IEnumerable interface, which means the constraint will check if the actual and expected collection contain the same values in the same order. The values contained by the collection can have properties and fields of their own that will be checked as well because of the recursive nature of the constraint.
Indexed properties are not supported, they will just be skipped.
I have supplied a patch for Rhino Mocks today and I am hoping Ayende will accept it for the next release of Rhino Mocks.
Download code This is a Visual Studio 2005 solution containing the constraint code and unitttests.
| November '07 | ||||||
| Mo | Tu | We | Th | Fr | Sa | Su |
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | ||