English 中文(简体)
a. 除第2.6号国家清单外的检查财产
原标题:Check property of an exception with NUnit 2.6
  • 时间:2011-09-02 18:46:49
  •  标签:
  • c#
  • nunit

What is the most idiomatic way with NUnit 2.6 to check equality of a property of an exception?

守则一

Assert.That(() => someObject.MethodThrows(),
  Throws.TypeOf<SomeException>().With.Property("Data").Count.EqualTo(3), /* Data is a collection */
  "Exception expected");

我可以使用<代码>Assert表示,但这似乎过于复杂和不必要的:

  Assert.AreEqual(3,
    Assert.Throws<SomeException>(
      () => someObject.MethodThrows(),
      "Exception expected").Data.Count);

<><>>> 事实上,第一部法典就是行之有效的。 我不知道为什么在提出这一问题之前没有几次工作。

最佳回答

我可以谈一下六大特,但就五大特而言,我可以谈一下:

Public Class MyException
    Inherits Exception
    Public Property SomeList As New List(Of String) From {"hello", "world"}
End Class

<TestFixture()>
Public Class TestClass1
    Public Shared Sub DoSomething()
        Throw New MyException()
    End Sub

    <Test()>
    Public Sub TestExample()
        Assert.That(Sub() DoSomething(), Throws.TypeOf(Of MyException)().With.Property("SomeList").Count.EqualTo(3))
    End Sub
End Class

produces this following error message:

Expected: <ClassLibrary1.MyException> and property SomeList property Count equal to 3
But was:  < "hello", "world" >

难道这只是六国联会的倒退吗?

问题回答

我将这样做:

var exception = Assert.Throws<SomeException>(() => someObject.MethodThrows(),
                                             "Exception expected")
Assert.AreEqual(3, exception.Data.Count);

这是你最清楚的:

  • Unlike your first example, this is refactoring safe
  • It asserts one thing at a time, not multiple like both of your examples.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签