Using AssertThrows in C++/CLI

451 views
Skip to first unread message

t2n

unread,
Sep 27, 2012, 11:35:33 AM9/27/12
to nunit-...@googlegroups.com
Hello,

are there any examples how to use Assert.Throws methods in C++/CLI. Especially for the case when a function with parameters throws an exception. Thanks in advance.

Best regards,

Tommi K.

Michael

unread,
Sep 27, 2012, 11:48:40 AM9/27/12
to nunit-...@googlegroups.com

Don't let the C++/CLI thing fool you: CLI is the C++-y CLI syntax-y issues, it's nothing more than a bridge into .NET.

 

So the same exceptions you have access to in .NET (i.e. C#, NUnit, etc) are there for NUnit C# test code.

 

HTH

 

Best regards,

 

Michael

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nunit-discuss/-/_EkKdVMbQ7UJ.
To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.

Michael Powell

unread,
Sep 27, 2012, 11:50:15 AM9/27/12
to nunit-...@googlegroups.com

Don't let the C++/CLI thing fool you: CLI is the C++-y CLI syntax-y issues, it's nothing more than a bridge into .NET.

So the same exceptions you have access to in .NET (i.e. C#, NUnit, etc) are there for NUnit C# test code.

 HTH

 Best regards,

 Michael

Charlie Poole

unread,
Sep 27, 2012, 12:25:06 PM9/27/12
to nunit-...@googlegroups.com
Hi Tommi,

Here's a super-simple example to get you started...

[Test]
void ThrowsTests()
{
Assert::Throws(System::ArgumentException::typeid, gcnew
TestDelegate(ThrowsArgumentException));
}

static void ThrowsArgumentException()
{
throw gcnew System::ArgumentException();
}

I've been away from C++ for a while but AFAIK there is no anonymous
delegate or .NET lambda available. If I'm wrong, somebody else please
jump in here!

Charlie

Michael

unread,
Sep 27, 2012, 12:43:01 PM9/27/12
to nunit-...@googlegroups.com

Yessir, this is *one* way to do it. When I've worked with C++/CLI, I like to get away from C++ as fast as possible, at least for reason(s) Charlie mentioned: like lambdas. While you *can* get into some trouble with the C++ language more (differently) than with C#, it's pretty hard to get the CLI *that* wrong that you can't make the leap to test in C#. One thing, you *can* get to IEnumerable extension methods the long way round, but it's much nicer to test at the C# level. There are libraries that help with this, but again, I recommend testing in C# and avoid all that. Anyway, that's my two cent recommendation.

Tommi Kaksonen

unread,
Sep 28, 2012, 3:10:33 AM9/28/12
to nunit-...@googlegroups.com
Hi Charlie and Michael,

thanks for the responses, I tried to implement it the hard way, here is how I should do it:

[Test]
void Test()
{
// At first I thought implementing test logic here and passing
// result object as a parameter for the ThrowsArgumentException
Assert::Throws(System::ArgumentException::typeid, 
gcnew TestDelegate(ThrowsArgumentException));
}

static void ThrowsArgumentException()
{
// The test logic should be implemented here
throw gcnew System::ArgumentException();
}

-Tommi

2012/9/27 Michael <mwpow...@gmail.com>
To view this discussion on the web visit https://groups.google.com/d/msg/nunit-discuss/-/Enb3g8URqBYJ.

Charlie Poole

unread,
Sep 28, 2012, 11:53:22 AM9/28/12
to nunit-...@googlegroups.com
Exactly!
Reply all
Reply to author
Forward
0 new messages