-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EXPECT_THAT with Throws invokes callable twice when exception is not thrown #4073
Comments
Just wanted to chip in after digging through the source code and inspecting the compiled assembly (I'm not a maintainer of this code): It appears this is expected behavior based on the source code in https://github.com/google/googletest/blob/main/googlemock/include/gmock/gmock-matchers.h. The
Inside the Lines: 1600 - 1607
Only when the assertion fails, then is the Lines 1591-1593
Hope this helps somebody! |
The cause is clear enough, but it's not clear to me how feasible it is to avoid it unfortunately. I tried what looked like an obvious fix, but it seems trickier than I thought in the general case. I'll keep the issue open for now in case others have thoughts, but I'm not sure when I might get to take another look. If you find a fix you're also welcome to open a pull request. |
Out of curiosity - what was the "obvious fix"? |
Removing the |
By the way, have you tried using
Is this adequate for what you're trying to do? Sorry for the lack of a better fix—in the meantime, though I would suggest using that as a workaround. If you run into this issue somewhere where |
Thank you for providing some context. Yes, I tried I have other workarounds (like re-creating the full state inside the lambda body), which should help in most of my cases, but this is obviously not ideal. |
Ah I see. I'll keep this issue open in case someone can find a solution at some point, but unfortunately you'll have to work around it for now. Thanks! |
The same issue occurs when exception doesn't match condition (for example text is different). UPD:
|
It has just helped me indeed, thanks! I had changed my error messages, then a test was failing, and it was driving me crazy why the callable was being invoked twice. |
My workaround:
Usage:
|
Looking into this bug, and just querious about this test case. Seems it is designed to have a second chance here. Is this truly a bug or a design choice? I am just wondering what's the original reason to have this second chance here. Is that plan to work for any special case? |
I've been running into this issue also. In my case, my lambda function uses |
@huang325, I believe it's the latter. The documentation says:
Also here. |
Describe the bug
EXPECT_THAT
macro with theThrows
expectation invokes the callable twice when it does not throw an exception. This does not happen when the exception is thrown.This causes failures for stateful lambdas or lambdas with side effects.
Steps to reproduce the bug
Repro code:
Outputs:
Also see https://godbolt.org/z/WE44hn5Kc.
Does the bug persist in the most recent commit?
Yes.
What operating system and version are you using?
Does not matter. The issue happens on both Windows and Linux.
What compiler and version are you using?
MSVC 17.4.1, clang trunk.
The text was updated successfully, but these errors were encountered: