I just started using CATCH and I like it (easy to start, nicely readable test cases, small addition to project).
However, I ran into some difficulty when checking whether the construction of some object fails with an expected exception.
For some project I implemented an easy workaround to have named and optional arguments for certain class constructors (I have a large number of arguments which all need to be optional): they simple expect a std::map (type called Args) containing my arguments.I tested that the following lines work.
MyClass x = {Args {{"expected_arg", "value"}}}; // works fine
MyClass y = {Args {{"buggy_arg", "value"}}}; // throws expected exception
However, I fail to write a CATCH test for the second line.
Here is something I tried:
REQUIRE_THROWS( MyClass {Args {{"buggy_arg", "value"}}}; )
This causes a compiler errors: "Use of undeclared identifier REQUIRE_THROWS". Note that this happens in a file where CATCH was used successfully before -- this error is seemingly caused somehow by the argument to the macro REQUIRE_THROWS. I tried a few variants of the above line, but could not get around that error.
BTW: The same error happens for the following line which simplifies the case above.
REQUIRE_THROWS( std::vector<std::string> {"this", "is", "a", "test"}; );
Does CATCH perhaps not yet support C++11 initializer lists in its assertion macros?
Can you perhaps suggest any alternative way to do the test above? Thank you very much!
Kind regards,
Torsten
PS: I am new to C++ (used other languages before), so apologies in case there is some stupid misunderstanding of the language.