Search code examples
exceptiontestingnunitc++-cliexpected-exception

How do I use ExpectedException in C++/CLI NUnit tests?


How do you do the equivalent of:

[Test, ExpectedException( typeof(ArgumentOutOfRangeException) )]
void Test_Something_That_Throws_Exception()
{
    throw gcnew ArgumentOutOfRangeException("Some more detail");
}

...in C++ (the example there is C#)? As far as I can see, there's no typeof() function for the C++ implementation of NUnit.


Solution

  • To avoid anyone else hunting around for ages trying to find it, here's the solution:

    [Test, ExpectedException( ArgumentOutOfRangeException::typeid )]
    void Test_Something_That_Throws_Exception()
    {
         throw gcnew ArgumentOutOfRangeException("Some more detail");
    }
    

    Simply use the ::typeid of the exception :-)