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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s