Re: Test exception

Greg Fleming <[email protected]>
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <[email protected]>
[email protected] wrote:
> Can somebody give me an example of testing exception in C++?
>
> I tried use the same way as we did in C# but it does not work:
> [Test]
> [ExpectedException(typeof(std::bad_alloc))]
> void TestCurrentChannel();
>   

I imagine this is the same basic problem as your previous question about 
Assert::IsNull, the difference between managed and native types.  
ExpectedException wants a managed type, i.e. a ref class or struct that 
gets created on the managed heap with gcnew.  std::bad_alloc is a native 
type, which gets created on the stack or on the unmanaged heap with 
normal C++ new, and you'll have to test for it the old fashioned way.  
In code:

[Test]
[ExpectedException(typeof(System::ApplicationException))]
void TestManagedException()
{
    throw gcnew System::ApplicationException();
}

[Test]
void TestNativeException()
{
    bool thrown = false;
    try { throw std::bad_alloc(); }
    catch( std::bad_alloc& ) { thrown = true; }
    Assert::IsTrue( thrown );
}

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.