Visual Studio ユニット テスト ケースを使用しています。テスト中のメソッドから Argument Exception が予想されるユニット テスト ケースを作成しましたMethodUnderTest
。テスト ケースの他の部分 (セットアップ部分) が予期される例外をスローしたArgumentException
場合、テスト ケースが失敗するように強制したいとします。instance.MethodUnderTest();
セットアップが正しく、コード行がスローされる場合にのみ合格する必要がありますArgumentException
。
を使用して達成できますtry catch
が、これを達成するためのより良い方法があるか知りたいです。
[ExpectedException(typeof(ArgumentException))]
public void TestCaseMethod()
{
// Set up
Mock<ITestClass> testM = new Mock<ITestClass>();
AnimalClass instance = new AnimalClass(testM.Object);
// call the method under test
instance.MethodUnderTest();
}