Moq:测试是否抛出异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Moq:测试是否抛出异常相关的知识,希望对你有一定的参考价值。

The ExpectedException section under [TestMethod] is where you tell Moq what type of exception your listening for. If that exception is thrown, then this test will pass and it will stop running all the code under the target.CalculateProvisionalPrice.

In case the exception isn't thrown, the Assert.Fail will throw an exception alerting you that the ArgumentNullException wasn't thrown.
  1. [TestMethod]
  2. [ExpectedException(typeof(ArgumentNullException))]
  3. public void PricingService_CalculateProvisionalPrice_NullReference_Test()
  4. {
  5. // Action
  6. target.CalculateProvisionalPrice(null);
  7.  
  8. Assert.Fail("Should have thrown ArgumentNullException");
  9. }

以上是关于Moq:测试是否抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

Moq:Mock.Verify()抛出NullReferenceException

PHPUnit 断言抛出异常?

Lambda查询抛出对象不包含错误,同时通过`MOQ`设置来获取动态类型的数据

片段中的getView()导致抛出异常,不确定原因

java抛出异常--后续代码是否还会执行

TestCases 如何测试抛出的不同异常?