How to avoid accessing local variables from ON_CALL/EXPECT_CALL action during teardown #4496
Unanswered
Griffon26
asked this question in
Community Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have some tests that use
EXPECT_CALL
to program some mock methods to write to certain local variables.After the test has ended the teardown can still cause some more calls to these mocks, but the local variables have already gone out of scope, resulting in access violations.
We have tried using
Mock::VerifyAndClearExpectations(&mock_obj)
, but that doesn't seem to remove the actions set with EXPECT_CALL.The cheat sheet says that
Mock::VerifyAndClear(&mock_obj)
also removes the default actions set by ON_CALL(), but that's not what we want, because we need some of the ON_CALL behaviors during teardown.We also don't want to make all of these variables members of the fixture, because the fixture should not be concerned with what may be needed for one specific test.
We are now considering using shared_ptr instead of references to local variables, but this is easy to forget about when writing new tests.
What is the best way to avoid these types of access violations during the teardown?
Beta Was this translation helpful? Give feedback.
All reactions