Check exception property with NUnit 2.6

What is the most idiomatic way to use NUnit 2.6 to check if an exception property is equal?

The code I would like to write but does not work: Expected 3, but was <empty>

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

I could use Assert nested expressions, but this seems too complicated and unnecessary:

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

edit Actually the first code sample works. I do not know why it did not work several times before posting this question.

+7
source share
2 answers

I cannot speak with NUnit 2.6, but on NUnit 2.5 the following test:

 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 

displays the following error message:

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

Could this be a regression in NUnit 2.6 beta?

+1
source

I would go with this:

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

This is the best you can get:

  • Unlike your first example, this is safe refactoring.
  • He argues one at a time, and not several, as both of your examples.
+1
source

All Articles