views:

49

answers:

2

Hi 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?

Thanks

+3  A: 

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.

Mark Rushakoff
I'm not sure what all of this () => pointStore.Store(new[] { firstPoint }) is for. Is there not a single line that I can put into my test function?
Martin
Thank you for the extra info Mark, you helped to make it really clear to me, thank you!
Martin
+1  A: 

Here's a simple example using both ways.

string test = null;
Assert.Throws( typeof( NullReferenceException ), () => test.Substring( 0, 4 ) );
Assert.Throws<NullReferenceException>( () => test.Substring( 0, 4 ) );

If you don't want to use lambdas.

[Test]
public void Test()
{
    Assert.Throws<NullReferenceException>( _TestBody );
}

private void _TestBody()
{
    string test = null;
    test.Substring( 0, 4 );
}
Jerod Houghtelling