I'm new to using Moq
with Xunit
in Visual Studio 2019.
I want to mock the contructor call of a class that is being called in the contructor of my tested class. A simple demonstration of my problem:
public class MockedClass : IMockedClass
{
public MockedClass()
{
// don't call this
}
public List<string> Function1()
{ /* return a List<string> here */ }
}
public class MyClass
{
readonly IMockedClass mockedClass = null;
public MyClass()
{
mockedClass = new MockedClass();
}
public string Function2()
{ /* return a string here */ }
}
public class MyClassTest
{
[Fact]
public string Function2Test()
{
var returnMock = new List<string>() { "1", "2", "3" };
var mockMockedClass = new Mock<IMockedClass>();
mockMockedClass.Setup(x => x.Function1()).Returns(returnMock);
var myClass = new MyClass();
var result = myClass.Function2();
...
}
}
My problem is, that in the test Function2Test
the constructor of MyClass
is being called that calls the constructor of MockedClass
.
But I don't want the constructor of MockedClass
being called.
How can I mock the constructor of MockedClass
?