tags:

views:

27

answers:

4

When I'm testing my C++ class with Boost.Test and my custom exceptions are thrown (they are instances of my class), this is the message I see in log:

unknown location:0: fatal error in "testMethod": unknown type

It's very un-informative and I don't know how to teach Boost.Test to convert my exception to string and display it properly. My Exception class has operator string(), but it doesn't help. Any ideas? Thanks!

+1  A: 

You may need to define an operator<< in the std namespace:

namespace std {
    inline std::ostream& operator<<(std::ostream& os, const Exception& ex) {
        os << ex.string();
        return os;
    }
}

This should allow boost.test to display the contents of your exception.

I've found this necessary so that objects can be tested with BOOST_CHECK_EQUAL(), etc.

Ferruccio
+1  A: 

You can test if a function throw a specified except by using BOOST_CHECK_THROW or similar

see Boost.Test Docs:

class my_exception{};

BOOST_AUTO_TEST_CASE( test )
{
   int i =  0;
   BOOST_CHECK_THROW( i++, my_exception );
}
Lars
A: 

I believe it would work if your custom exception class inherited from std::exception.

Space_C0wb0y
Actually I inherited from `std::exception` **and** `std::string` :) Thanks
Vincenzo
A: 

I just inherited it from std::string and everything works fine.

Vincenzo