現在、レガシー Windows フォーム アプリケーションのシステム テストを書いています。
私のテストでは、Program.Main()
メソッドを呼び出します。メソッドは、Application.Run(new MainForm());
ある時点で呼び出します。
Application.Run() を制御できるものに置き換える方法はありますか?
具体的には、実行を停止して未処理の例外をキャッチできる必要があります。
現在、レガシー Windows フォーム アプリケーションのシステム テストを書いています。
私のテストでは、Program.Main()
メソッドを呼び出します。メソッドは、Application.Run(new MainForm());
ある時点で呼び出します。
Application.Run() を制御できるものに置き換える方法はありますか?
具体的には、実行を停止して未処理の例外をキャッチできる必要があります。
Program.Main を変更して、フォームを入力パラメーターとして受け入れるようにできます。デフォルトは MainForm です。参照型は null 以外のデフォルトを持つことはできませんが、2 つのプロトタイプを使用して同じことを達成できます。
class Program
{
//Normal entry point
static public void Main()
{
Main(new MainForm());
}
//Internal & test entry point
static public void Main(Form form)
{
DoSomeSetup();
Application.Run(form);
}
}
通常の方法でプログラムを実行すると、MainForm
.
ただし、テスト プロジェクトから実行する場合は、次のように呼び出すことができます。
Program.Main(new FormICanControl());
そして、あなたはそれを制御することができます。
//Arrange
var t = new TestForm();
//Act
Program.Main(t);
t.ExecuteSomeTest();
//Assert
Assert.AreEqual(t.ResultCode, 0, "Test failed.");