How do I get shims for base classes using Microsoft Fakes?

kalrashi picture kalrashi · Feb 17, 2013 · Viewed 9.9k times · Source
class Parent{
   public string Name{ get; set; }
}

class Child :Parent{
   public string  address{ get; set; }
}

[TestClass]
class TestClass{
   [TestMethod]
   public void TestMethod()
   {
      var c = new Fakes.Child();
      c.addressGet = "foo"; // I can see that
      c.NameGet = "bar"; // This DOES NOT exists
   }
}

How can I set the "name" in the above code sample?

Answer

Jesus is Lord picture Jesus is Lord · Feb 22, 2014

The generated class for Parent will have a constructor that looks like: ShimParent(Parent p).

All you need to do is:

var child = new ShimChild();
var parent = new ShimParent(child);

And set the appropriate values on the respective Shim's.