jabk
jabk

Reputation: 1468

googletesting out of range

I have a vector<string> and a method(inside a class) that returns a character at a certain position. This is all happening in try catch block.

The problem appears when I try to google test this method, purposely invoking the std::out_of_range exception with EXPECT_THROW(method, std::out_of_range), but instead of getting a passed test I get several .what() error messages vector::_M_range_check and a test failure.

mentioned method

char ClassA::getC(int x, int y){
    try{
         return vec.at(y).at(x);

    catch(const out_of_range& e){
         cout << '\n' << e.what() << '\n';
         return 0;
    }
}

There's also a similar void method that sets a different char at a certain position where I just call vec.at(y).at(x) = c; and where I'd also like to catch std::out_of_range

So what am i doing wrong?

Upvotes: 0

Views: 632

Answers (1)

Karoly Horvath
Karoly Horvath

Reputation: 96286

EXPECT_THROW(method, std::out_of_range)

What you're saying here is that you expect the method to throw this exception. That's what is wrong.

That'll never happen because that exception is handled in the method.

The error messages come from cout << ....

On a more fundamental level, your tested function is wrong. That's not the way you want to handle exceptions in your application.

Upvotes: 1

Related Questions