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?
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 fromIAsset.Info.Another option is to make
SerializableNodeTreeimplement an interface that has all the methods you want to mock, and haveIAsset.Inforeturn that interface type instead of aSerializableNodeTreedirectly.