私はいくつかの外部依存関係のモックに取り組んでおり、別のサードパーティ クラスのインスタンスをコンストラクターに取り込むサードパーティ クラスで問題が発生しています。SOコミュニティが私に何らかの方向性を与えてくれることを願っています。
SomeRelatedLibraryClass
のモックインスタンスをコンストラクターに取り込むのモックインスタンスを作成したいと思いますSomeLibraryClass
。どうすればSomeRelatedLibraryClass
このように嘲笑できますか?
リポジトリコード...
これは、テスト コンソール アプリケーションで使用している Main メソッドです。
public static void Main()
{
try
{
SomeLibraryClass slc = new SomeLibraryClass("direct to 3rd party");
slc.WriteMessage("3rd party message");
Console.WriteLine();
MyClass mc = new MyClass("through myclass");
mc.WriteMessage("myclass message");
Console.WriteLine();
Mock<MyClass> mockMc = new Mock<MyClass>("mock myclass");
mockMc.Setup(i => i.WriteMessage(It.IsAny<string>()))
.Callback((string message) => Console.WriteLine(string.Concat("Mock SomeLibraryClass WriteMessage: ", message)));
mockMc.Object.WriteMessage("mock message");
Console.WriteLine();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
これは、1 つのサードパーティ クラスをラップして Moq できるようにするために使用したクラスです。
public class MyClass
{
private SomeLibraryClass _SLC;
public MyClass(string constructMsg)
{
_SLC = new SomeLibraryClass(constructMsg);
}
public virtual void WriteMessage(string message)
{
_SLC.WriteMessage(message);
}
}
私が使用しているサードパーティ クラスの 2 つの例を次に示します (これらは編集できません)。
public class SomeLibraryClass
{
public SomeLibraryClass(string constructMsg)
{
Console.WriteLine(string.Concat("SomeLibraryClass Constructor: ", constructMsg));
}
public void WriteMessage(string message)
{
Console.WriteLine(string.Concat("SomeLibraryClass WriteMessage: ", message));
}
}
public class SomeRelatedLibraryClass
{
public SomeRelatedLibraryClass(SomeLibraryClass slc)
{
//do nothing
}
public void WriteMessage(string message)
{
Console.WriteLine(string.Concat("SomeRelatedLibraryClass WriteMessage: ", message));
}
}