Skip to content

Instantly share code, notes, and snippets.

@miklund
Last active January 15, 2016 19:44
Show Gist options
  • Save miklund/e40b96a56865f0e0c9fa to your computer and use it in GitHub Desktop.
Save miklund/e40b96a56865f0e0c9fa to your computer and use it in GitHub Desktop.
2009-05-30 New ExpectedException model in NUnit 2.5
# Title: New ExpectedException model in NUnit 2.5
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2009/05/30/new-expectedexception-model-in-nunit-25.html
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void NullReferenceExceptionTestInMsTest()
{
/* Setup */
string[] names = null;
int numberOfNames = 0;
/* Test */
numberOfNames = names.Length;
}
[Test(Description = "Testing the new ExpectedException functionality in NUnit 2.5")]
public void NullReferenceExceptionTest()
{
/* Setup */
string[] names = null;
int numberOfNames = 0;
/* Assert */
Assert.Throws<NullReferenceException>(
/* Test */
() => numberOfNames = names.Length);
/* Assert that numberOfNames is still zero */
Assert.That(numberOfNames == 0);
}
[Test(Description = "Testing the new ExpectedException functionality in NUnit 2.5")]
public void NullReferenceExceptionTest()
{
/* Setup */
string[] names = null;
int numberOfNames = 0;
/* Test */
TestDelegate throwingCode = () => numberOfNames = names.Length;
/* Assert */
Assert.Throws<NullReferenceException>(throwingCode);
/* Assert that numberOfNames is still zero */
Assert.That(numberOfNames == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment