Test Expected Exception with NUnit 2.5

Testing the expected exceptions has been improved a lot by NUnit 2.5, let says you have a scenario that a method checks for the input parameters and throws some ArgumentNullException exceptions, it was kind of cumbersome to cover all those cases in your tests, normally, you will have multiple test methods using ExpectedExceptionAttribute. But with NUnit 2.5.x, we can write something likes


[Test]

public void AndSpecification_Construction_With_Null_Inputs_Test()

{

   // Arrange
   ISpecification<MockEntity> spec = new Moq.Mock<ISpecification<MockEntity>>().Object;

   // Passing null for the left specification
   Assert.That(() => new AndSpecification<MockEntity>(null, spec),
      Throws.TypeOf<ArgumentNullException>()
         .With.Property("ParamName").EqualTo("left"));

   // Passing null for the right specification
   Assert.That(() => new AndSpecification<MockEntity>(spec, null),
      Throws.TypeOf<ArgumentNullException>()
         .With.Property("ParamName").EqualTo("right"));

}

One single method can handle multiple cases.

You can find more details information from http://nunit.net/blogs/?p=63