RhinoMocks Stub not retaining value when property shadowed on inheriting interface

I cannot remember having a Sprint like this! First the issue with debugging in VS2008 on Windows7 x64, then the issue with BadImageFormatException throwing when inheritance constraints were applied to a generic method, and now this one!

 

The Problem

 

I was still fixing the tests that had failed after upgrading to Castle.Windsor 2.1 and RhinoMocks 3.6 when I stumble on this one. Basically I had a unit test that passed before I upgraded and as soon as I switched to using the most recent versions of both libraries the test failed.

The issue manifested on a stub for an Interface that inherited from another interface re-implementing a property member with the keyword new (not a recommended approach but necessary because we were handling legacy code). Here is an example of how our interfaces are designed:

public interface INamedObject
{
   string DisplayName {get;set;}
}

public interface IFeature : INamedObject
{
   new string DisplayName {get;set;}
}

There is a base interface that implements a property which is being shadowed by the inheriting interface.

At some point in our unit test we need to access DisplayName by its base interface INamedObject. Something like the following:

 [TestMethod] 
 public void TestMethodThatFails() 
 { 
     var feature = MockRepository.GenerateStub<IFeature>(); 
     feature.DisplayName = "some name";

     Assert.AreEqual("some name", feature.DisplayName); 
     Assert.AreEqual("some name", ((INamedObject)feature).DisplayName); 
 }

It seems that in the previous Castle.DynamicProxy implementation the proxy created for such interface would result in DisplayName being available on both base interface and inheriting interface. With the new version, when we try accessing DisplayName after casting the object to INamedObject we get a null value. The only way to access it is through the inheriting interface.

It is my understanding that this is not the expected behavior for a stub. I would expect it to retain the value as a regular class does when it implements IFeature.

 

A workaround

 

In the end I had to explicitly set DisplayName value on feature by previously casting it to INamedObject, likewise:

 [TestMethod] 
 public void TestMethodThatPasses() 
 { 
     var feature = MockRepository.GenerateStub<IFeature>(); 
     feature.DisplayName = "some name"; 
     ((INamedObject)feature).DisplayName = "some name";

     Assert.AreEqual("some name", feature.DisplayName);
     Assert.AreEqual("some name", ((INamedObject)feature).DisplayName);
 }

It is true that we need to keep up with the evolution of libraries we consume in our projects, but has more tests are created the price we have to pay for this inconsistencies between versions of the same library starts to raise. It is always up to us to assure that the effort is worth it, and in this case, now that I have migrated all the tests, it was not that bad after all. 🙂

Shout it kick it on DotNetKicks.com

3 thoughts on “RhinoMocks Stub not retaining value when property shadowed on inheriting interface

  1. Pingback: DotNetShoutout

Leave a Reply

Your email address will not be published. Required fields are marked *