我正在使用 Visual Studio 单元测试用例。我已经编写了单元测试用例,其中 Argument Exception 预期来自被测方法MethodUnderTest
。假设如果测试用例的任何其他部分(设置部分)抛出预期的异常ArgumentException
,那么我想强制我的测试用例应该失败。只有在设置正确并且instance.MethodUnderTest();
代码行抛出时,它才应该通过ArgumentException
。
我可以实现 using 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();
}