如何包含内部方法

发布于 2024-09-13 20:58:31 字数 129 浏览 0 评论 0原文

我不想使用 [ExpectedException(ExceptionType = typeof(Exception), ExpectedMessage = "")] 而想将异常包含在我的方法中。我可以这样做吗?请举个例子。

谢谢

I don`t want to use the [ExpectedException(ExceptionType = typeof(Exception), ExpectedMessage = "")] instead would like to include the exception inside my method. Can I do that? Any example please.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋你朝朝暮暮 2024-09-20 20:58:31

有时我想测试特定异常属性的值,在这种情况下,我有时选择不使用 ExpectedException 属性。

相反,我使用以下方法(示例):

[Test]
public void MyTestMethod() {
   try {
      var obj = new MyClass();
      obj.Foo(-7); // Here I expect an exception to be thrown
      Assert.Fail(); // in case the exception has not been thrown
   }
   catch(MySpecialException ex) {
      // Exception was thrown, now I can assert things on it, e.g.
      Assert.AreEqual(-7, ex.IncorrectValue);
   }
}

Sometimes I want to test for the value of particular exception properties and in that case I sometimes choose to not use the ExpectedException attribute.

Instead I use the following approach (an example):

[Test]
public void MyTestMethod() {
   try {
      var obj = new MyClass();
      obj.Foo(-7); // Here I expect an exception to be thrown
      Assert.Fail(); // in case the exception has not been thrown
   }
   catch(MySpecialException ex) {
      // Exception was thrown, now I can assert things on it, e.g.
      Assert.AreEqual(-7, ex.IncorrectValue);
   }
}
慕烟庭风 2024-09-20 20:58:31

你的问题不太有意义。直觉上,我猜您是在问让单元测试中捕获异常,然后即使异常已引发,您也可以执行断言?

[TestMethod]
public void Test1()
{
  try{
    // You're code to test.
  }
  catch(Exception ex){
   Assert.AreEqual(1, 1); // Or whatever you want to actually assert.
  }
}

编辑:

或者

[TestMethod]
public void Test1()
{
  try{
    // You're code to test.
    AreEqual(1, 1); // Or whatever you want to actually assert.
  }
  catch(Exception ex){
   Assert.Fail();
  }
}

Your question doesn't quite make sense. As a hunch, I guess you're asking about letting an exception be caught in your unit test, and then you can perform an assertion even though the exception has been raised?

[TestMethod]
public void Test1()
{
  try{
    // You're code to test.
  }
  catch(Exception ex){
   Assert.AreEqual(1, 1); // Or whatever you want to actually assert.
  }
}

EDIT:

Or

[TestMethod]
public void Test1()
{
  try{
    // You're code to test.
    AreEqual(1, 1); // Or whatever you want to actually assert.
  }
  catch(Exception ex){
   Assert.Fail();
  }
}
原来是傀儡 2024-09-20 20:58:31

像这样的东西:

[TestMethod]
public void FooTest()
{
  try
  {
    // run test
    Assert.Fail("Expected exception had not been thrown");
  }
  catch(Exception ex)
  {
    // assert exception or just leave blank
  }
}

something like this:

[TestMethod]
public void FooTest()
{
  try
  {
    // run test
    Assert.Fail("Expected exception had not been thrown");
  }
  catch(Exception ex)
  {
    // assert exception or just leave blank
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文