Take the 2-minute tour ×

This is not really specific to Moq but more of a general Mocking framework question. I have created a mock object for an object of type, "IAsset". I would like to mock the type that is returned from IAsset 's getter, "Info".

 var mock = new Mock<IAsset>();
        mock.SetupGet(i => i.Info).Returns(//want to pass back a mocked abstract);
        mock.SetupProperty(g => g.Id, Guid.NewGuid());

The problem I am running into is Mocking this returned property value.

 mock.SetupGet(i => i.Info).Returns(//this is the type I need to mock);

The property holds an abstract type. This type extends XDocument.

public abstract class SerializableNodeTree : XDocument, ISerializable{...}

So.. what I would like to do is this:

 var nodeTreeMock = new Mock<SerializableNodeTree>();
        nodeTreeMock .SetupGet(d => d.Document).Returns(xdoc);

xdoc is a XDocument instance. This will not work because the XDocument.Document getter is not virtual. Which makes sense.

Should I just hand code a mock that is derived from SerializableNodeTree or is this there a way to Mock this object?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

In a case like this, I would treat XDocument as a standard, non-mockable object like strings and most POCOs and native types. That is to say, you should create a real (non-mocked) SerializableNodeTree to return from IAsset.Info.

Another option is to make SerializableNodeTree implement an interface that has all the methods you want to mock, and have IAsset.Info return that interface type instead of a SerializableNodeTree directly.

share|improve this answer
I decided just to use your first suggestion and created a test implementation of SerializableNodeTree –  Nick Jul 29 '11 at 16:05

In this case I would create a test double that derives from your abstract class. That'll give you what you need in your test.

public class SerializableNodeTreeDouble : SerializableNodeTree
   public new XDocument Document


public void TestMethod()
   SerializableNodeTreeDouble testDouble = new SerializableNodeTreeDouble();
   testDouble.XDocument = xdoc; // your xdoc


Hope this helps.

share|improve this answer
If the Document property is not virtual, you can hide the original Document property, but you cannot override it. Therefore, I would expect that the mock class won't call the property getter that you have defined. –  StriplingWarrior Jul 28 '11 at 23:53
The new Modifier will hide the base classses implementation, thus allowing us to mock out the dependency here (not ideally though). Here is the MSDN for the new Modifier. msdn.microsoft.com/en-us/library/435f1dw2.aspx –  Brian Dishaw Jul 29 '11 at 10:59
Yes, the new modifier is what you'd use to hide the original Document property. But code that calls SerializableNodeTree.Document won't end up calling SerializableNodeTreeDouble.Document even if the underlying object is a SerializableNodeTreeDouble. In order to get that behavior you'd need to override the method, which can be done neither statically nor via Moq if the defined method is not abstract or virtual. –  StriplingWarrior Jul 29 '11 at 15:55
Right, if the code is called directly via the namespace it won't call the double. So what does the code here do? It is explictly calling SerializableNodeTree.Document or just Document property? If it's the second new should be just fine, right? (Not trying to beat a dead horse, just like the discussion) –  Brian Dishaw Jul 30 '11 at 0:30
@BrianDishaw Since it's typed as SerializableNodeTree in the mocked class, it will also call the property on that type, getting the original implementation. Hiding something using new doesn't really work well with polymorphism. –  MEMark Sep 8 at 21:09

Your Answer


