Rhino Mocks 2.4.2: The Unmocker
Okay, so I've a new release out (any time I push out a release, I just know that I'll have to have a new one ready in a short time).
So, what is the story here?
Some time ago I added the ability to easily create stub objects and dynamic mocks. So you could do something like this:
IList list = mocks.DynamicMock<IList>();
SetupResult.For(list.Count).Return(5);
mocks.ReplayAll();
//use list, list.Count will always return 5, anything else will return 0 or null
This works fine and I use it daily in my current project, but it's a problem where you get cases where you have a property on your mocked object, let's take this code:
public int SetId(Something something)
{
something.Prop = // Get the id
return something.Prop;
}
Now, what do you need in order to connect the setter and getter in this case? Well, before 2.4.2 you needed to know about the value beforehand, which was not always possible. Here is how you do it on 2.4.2, however:
[Test]
public void TestSetId()
{
Something something = mocks.DynamicMock<Something>();
SetupResult.For(something.Prop).CallOriginalMethod(); // for the getter
SetupResult.For(something.Prop = 0).CallOrigianlMethod(); // for the setter
Assert.AreEqual(5, objUnderTest.SetId(Something));
}
What do we have here? We create the mock object, and then we setup two calls, one for the property getter and another for the setter (the syntax may be a little weird for the setter, but it's actually the best syntax that we could think of). We tell Rhino that when it get a call to get_Prop, it should just call the original method on the object, and the same for set_Prop. That is a nice way to do it, I think.
Caveats:
- There is no either/or with this, it doesn't attempt to match arguments to see if it should call the original method or not, it just does.
- You can't set repeats or exceptions or return values, any call for this method will go to the original object.
- You can't use it on interfaces or abstract methods, since there isn't a method there to pass it to.
Comments
Comment preview