I am using an interface (IDataSet) in front of System.Data.DataSet for testing purposes. I want to be able to mock the Copy() method. Basically I want a copy/clone of the same mocked object.
Here’s some pseudo code of what I would like to do.
Mock<IDataSet> dataMock = new Mock<IDataSet>();
Mock<IDataSet> copyMock = ??? // How do I clone the mock?
dataMock.Setup(c => c.Copy()).Returns(copyMock.Object);
Is this possible?
Basically, a Mock is not the real thing, so it does not have real behavior. It’s not supposed to have real behavior – it’s supposed to do whatever you tell it while keeping track of what happened. Nothing more and nothing less.
This means that you have to tell it how its Copy method works. If you do the following, that’s the implmementation the Copy method will have:
However, you can also do this:
and that, then, becomes the implementation of the Copy method. Remember: an interface is not a contract that states what the method should do; it only defines the signature of methods.
You were probably hoping to copy data from one IDataSet to another, but remember that a Mock is pure behavior; it has no data.
A couple of alternatives you can think about are the following:
You can read about Stubs, Mocks, Fakes and other unit testing design patterns in the excellent book xUnit Test Patterns.