I am using .CallBase() in order to execute the real code behind the mock for the .Export() method from within the TsExporter class.
On the other side, the .RetrieveTranslations() is executing the real code too, and not returning the mocked value as "I told it to do it".
Here is the code:
[TestFixture]
public class TestClass
{
private Mock<ITsExporter> _tsExporter;
[SetUp]
public void SetUp()
{
_tsExporter = new Mock<TsExporter>().As<ITsExporter>();
//This is calling the real code which is good
_tsExporter.Setup(x => x.Export(It.IsAny<TsFileModel>(), It.IsAny<string>()))
.CallBase();
//but this is calling the real code too, and I was expecting not to
//call it and return the mock instead...
_tsExporter.Setup(x => x.RetrieveTranslations(It.IsAny<DataTable>(),
It.IsAny<string>(), It.IsAny<string>()))
.Returns(new DataTable());
}
[Test]
public void Test()
{
_tsExporter.Object.Export(new TsFileModel(), "");
}
}
What am I missing ?
Thank you !
Mock
s wrong. Take a look at this blog to understand how to design your code for unit tests and mocking. Basically you inject external dependencies (ITsExporter
) and the parent class will call your mocked object instead of the real object. – Simply Ged May 16 '19 at 5:45Export
is pointless as all it does itCallBase()
. You might as well remove that because_tsExporter.Object.Export(...)
will call the real method anyway. You gain nothing by mocking it. And, when you move that, you can probably see why your mock ofRetrieveTranslations
isn't getting called? HINT: You are just calling the real class which has no knowledge of your mock :-) – Simply Ged May 16 '19 at 6:24