1

WPF C# アプリケーションの NUnit テスト コードを書いています。ここでは、MessageBox.Show(""); を持ついくつかのメソッドがありますが、コードでこれを処理する方法がわかりません。

解決策を提供して助けてください。

ありがとう、

4

1 に答える 1

5

テストでモックできる一種の MessageBoxService を作成できます。コード例は次のとおりです。

public class ClassUnderTest
{
    public IMessageBoxService MessageBoxService { get; set; }

    public void SomeMethod()
    {
        //Some logic

        MessageBoxService.Show("message");

        //Some more logic
    }
}

interface IMessageBoxService
{
    void Show(string message);
}

public class MessageBoxService : IMessageBoxService
{
    public void Show(string message)
    {
        MessageBox.Show("");
    }
}

次に、テストで、パブリック プロパティをモックするか、モックされたインスタンスを渡すコンストラクターを作成するかを選択できます。たとえば、Moq を使用する場合、テストは次のようになります。

[Test]
public void ClassUnderTest_SomeMethod_ExpectsSomtething()
{
    ClassUnderTest testClass = new ClassUnderTest();
    testClass.MessageBoxService = new Mock<IMessageBoxService>().Object;
    //More setup

    //Action

    //Assertion
}
于 2013-07-08T09:55:56.097 に答える