当一个成员被调用时,也可。
1 public interface ICalculator 2 { 3 int Add(int a, int b); 4 string Mode { get; set; } 5 } 6 7 [TestMethod] 8 [ExpectedException(typeof(Exception))] 9 public void Test_ThrowingExceptions_ForVoid()10 {11 var calculator = Substitute.For();12 13 // 对无返回值函数14 calculator.Add(-1, -1).Returns(x => { throw new Exception(); });15 16 // 抛出异常17 calculator.Add(-1, -1);18 }19 20 [TestMethod]21 [ExpectedException(typeof(Exception))]22 public void Test_ThrowingExceptions_ForNonVoidAndVoid()23 {24 var calculator = Substitute.For ();25 26 // 对有返回值或无返回值函数27 calculator28 .When(x => x.Add(-2, -2))29 .Do(x => { throw new Exception(); });30 31 // 抛出异常32 calculator.Add(-2, -2);33 }