NUnit expected exceptions

Martin picture Martin · Aug 4, 2010 · Viewed 46.5k times · Source

I've got a set of test cases, some of which are expected to throw exceptions. Because of this, I have have set the attributes for these tests to expect exceptions like so:

[ExpectedException("System.NullReferenceException")]

When I run my tests locally all is good. However when I move my tests over to the CI server running TeamCity, all my tests that have expected exceptions fail. This is a known bug.

I am aware that there is also the Assert.Throws<> and Assert.Throws methods that NUnit offers.

My question is how can I make use of these instead of the attribute I'm currently using?

I've had a look around StackOverflow and tried a few things none of which seem to work for me.

Is there a simple 1 line solution to using this?

Answer

Mark Rushakoff picture Mark Rushakoff · Aug 4, 2010

I'm not sure what you've tried that is giving you trouble, but you can simply pass in a lambda as the first argument to Assert.Throws. Here's one from one of my tests that passes:

Assert.Throws<ArgumentException>(() => pointStore.Store(new[] { firstPoint }));

Okay, that example may have been a little verbose. Suppose I had a test

[Test]
[ExpectedException("System.NullReferenceException")]
public void TestFoo()
{
    MyObject o = null;
    o.Foo();
}

which would pass normally because o.Foo() would raise a null reference exception.

You then would drop the ExpectedException attribute and wrap your call to o.Foo() in an Assert.Throws.

[Test]
public void TestFoo()
{
    MyObject o = null;
    Assert.Throws<NullReferenceException>(() => o.Foo());
}

Assert.Throws "attempts to invoke a code snippet, represented as a delegate, in order to verify that it throws a particular exception." The () => DoSomething() syntax represents a lambda, essentially an anonymous method. So in this case, we are telling Assert.Throws to execute the snippet o.Foo().

So no, you don't just add a single line like you do an attribute; you need to explicitly wrap the section of your test that will throw the exception, in a call to Assert.Throws. You don't necessarily have to use a lambda, but that's often the most convenient.