0

I'm trying to test a c++ code on an Nunit framework but I keep getting the following Exception

System.Runtime.InteropServices.SEHException : External Component has thrown an exception.

which is supposedly perfectly normal (I assume) anyway I wanna ignore it. (i.e. Use ExpectedException) This is my .h file

 [Test, Description("Tests if an Entity has been successfully Locked")]
 void test_LockOperation();

and the .cpp file

 void TestDmObstacles::test_LockOperation()
{
  lockVal = DbtoDmObstaclesAdapter::lock( CmnGuid::parseString( L"3B6DB8F8-4BA7-DD11-B6A7-001E8CDE165C" ) );
  //When lock is successful the lockVal is 0
  Assert::AreEqual(0, lockVal);
}

I wanna use ExpectedException but I don't know how to do it in c++. I tried the try/catch method as well but it didn't work (I just put the Assertion in the catch block)

PS: I can't use another framework it has to be Nunit

EDIT

Here is the try/catch approach I used

    void TestDmObstacles::test_LockOperation()
{
    try
    {
        lockVal = DbtoDmObstaclesAdapter::lock( CmnGuid::parseString( L"3B6DB8F8-4BA7-DD11-B6A7-001E8CDE165C" ) );
    }
    catch (...)
    {
        //Assert::Fail();
        Assert::AreEqual(0, lockVal);

    }
}
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
Reda
  • 470
  • 2
  • 11
  • 19
  • _"I can't use another framework it has to be Nunit"_ -- I'm still curious, *why*!? – stakx - no longer contributing Nov 15 '10 at 09:18
  • I'm at work and I'm not allowed to download or install any other applications that are not already on my machine!... so simple and stupid as that – Reda Nov 15 '10 at 09:24
  • Can you give the try/catch method you used. Catching an exception is really the only sensible way to test it's been thrown, but perhaps you haven't done it correctly. – beldaz Nov 15 '10 at 09:27

2 Answers2

2

Is the exception expected, or is the exception acceptable?

If it is expected, then your unit test framework should have some kind of API that allows you to state the expected exception, and to fail the test if it does not occur. A quick trawl through the documentation yields the incantation:

[ExpectedException( "System.ArgumentException" )]

(replace System.ArgumentException with the exception you're expecting.)

If the exception is merely acceptable, then I would say that either your code or your test is broken. A unit test is to test that expected things happen. If there is a result in your test that only may yield a particular result, then you are not testing a consistent view of the unit from test to test. Hence, you're not really testing it.

It might indicate, for example, that your code is leaking an unexpected exception that it should be handling instead.

Kaz Dragon
  • 6,681
  • 2
  • 36
  • 45
0

Your code sample doesn't match what you are trying to achieve : if the exception is expected, than catching it is not supposed to fail the test.

Note that I wouldn't recommend (at all) for the test to catch (...) : any thrown exception will induce the same test result, which I doubt is what you want.

icecrime
  • 74,451
  • 13
  • 99
  • 111
  • you are right icecrime I just copied it from another test that's why! I edited it now. As for the catch(..) I was just trying it out to know where the error is, I'm gonna change it once I find a good way to assert – Reda Nov 15 '10 at 09:46