Isolator Extensibility: Properties as Fields

Let’s say I have a class that contains properties:

I want to return my own values by faking them and set behavior. I’ll use WhenCalled:

I remember talking to someone about the basics of AAA API, and he wrote the following test (liberally translating to our example), which should do the same:

It looks more natural, doesn’t it? Setting properties, instead of using WhenCalled. But Isolator doesn’t support that (yet). Let’s add a function that makes the test pass:

Here’s the ActAsFields function:

First of all, everything this function uses is already in Isolator right now. This an example on how to extend Isolator (at least until we do…).

The function does some reflection magic, goes over all the properties and sets the behavior (using Reflective API) for the setters and getters to return the same values. It uses DynamicReturnValue as the return value to do the storing and retrieving.

DynamicReturnValue wraps a delegate, which is invoked when the method we set the behavior on is called. The delegate includes two parameters: p is the parameter list of the method called, and o is the reference to the instance it’s being called on. That means, in the Setter case, p[0] contains the value for the setter. In the Getter, it simply returns the stored value. For each property, there’s a different instance of fakePropertyValue, and so we get storage of the variable. Cool trick.

This simple, yet effective method (concocted in Eli‘s mind) allows you to specify objects’ properties to behave as fields. Look at the powerful combination with using recursive fakes:

The ability to grab an item in the chain, and set the value instead of using WhenCalled makes the test much more readable. And we’re all for that. Now, we’re looking for a catchy name, like Grab-n-Set, for this usage. But I’m sure you can come up with a better name.

Care to try?