NUnit 2016 Throws.TypeOf

c#, nunit, resharper, unit-testing

Solution

Test fails because the thrown exception is uncaught and prevents the test from exercising to completion.

Use `Assert.Throws<>` to assert the thrown exception

[Test]
public void WindowTest() {
    Assert.That(ThrowsSomething("dave"), Is.EqualTo(-1));
    Assert.Throws<ArgumentNullException>(() => ThrowsSomething(null));
}

or use a delegate so that the exception can be caught and handled by the assertion.

Assert.That(() => ThrowsSomething(null), Throws.Exception.TypeOf<ArgumentNullException>());

Problem

Why does this code throw an exception instead of passing the test? ``` public static int ThrowsSomething(string name) { if (name == null) throw new ArgumentNullException(nameof(name), "can't be null because that's silly"); return -1; } [Test] public void WindowTest() { Assert.That(ThrowsSomething("dave"), Is.EqualTo(-1)); Assert.That(ThrowsSomething(null), Throws.TypeOf<ArgumentNullException>()); } ``` Unit Test Sessions window shows this: WindowTest [0:00.066] Failed: System.ArgumentNullException : can't be null because that's silly Visual Studio 2015 with ReSharper Ultimate 2016.3 and NUnit 3.6.1

Original source