Re: Problem in using EXPECT_DEATH after EXPECT_CALL

2,121 views
Skip to first unread message
Message has been deleted

Aditya Tayade

unread,
Mar 11, 2013, 9:56:05 PM3/11/13
to googletes...@googlegroups.com
Can anyone please give some suggestions? Is there any other group where I can ask this?

Thanks,
--
Adita

On Friday, March 8, 2013 4:37:53 PM UTC-8, Aditya Tayade wrote:
Hello,

I have the following scenario --

I'm writing a death test to check for failures. I have to pass in a value to a variable in a mock object which i'm doing using EXPECT CALL.
EXPECT_CALL ( *mock, foo ( arg1, _ ) ).
.WillOnce ( DoAll ( SetArgumentPointee<1> ( fake_var_value>), Return ( true ) );

I'm then doing -
EXPECT_DEATH ( bar ( ) ) 
bar() in turn calls the foo().

When I run this test it fails on the EXPECT_CALL saying that the function foo() was never called with message --
Expected: to be called once
           Actual: never called - unsatisfied and active

But when I do the exact same thing and use EXPECT_FALSE(..) instead of EXPECT_DEATH(..) 
EXPECT_FALSE ( bar ( ) ) 
This code fatals correctly and I can see that foo() was in fact called correctly.

I'm not able to understand why the test behaves as expected using EXPECT_FALSE but in case of EXPECT_DEATH it never calls foo().

Can anyone please help out?

Thanks,
--
Aditya
 
 

Vlad Losev

unread,
Mar 12, 2013, 12:10:44 AM3/12/13
to Google C++ Testing Framework
From https://code.google.com/p/googletest/wiki/AdvancedGuide#Caveats: "Since statement runs in the child process, any in-memory side effect (e.g. modifying a variable, releasing memory, etc) it causes will not be observable in the parent process". If you put a statement in a DEATH_TEST() macro, whatever changes the statement makes in memory will not be visible outside of the macro. That includes the fact that foo() was called from bar().

My recommendation is to simply remove that EXPECT_CALL. Or, if you still need the behavior, change the WillOnce clause to WillRepeatedly.

On Fri, Mar 8, 2013 at 4:37 PM, Aditya Tayade <adit...@google.com> wrote:
Hello,

I have the following scenario --

I'm writing a death test to check for failures. I have to pass in a value to a variable in a mock object which i'm doing using EXPECT CALL.
EXPECT_CALL ( *mock, foo ( arg1, _ ) ).
.WillOnce ( DoAll ( SetArgumentPointee<1> ( fake_var_value>), Return ( true ) );

I'm then doing -
EXPECT_DEATH ( bar ( ) ) 
bar() in turn calls the foo().

When I run this test it fails on the EXPECT_CALL saying that the function foo() was never called with message --

Expected: to be called once
           Actual: never called - unsatisfied and active

But when I do the exact same thing and use EXPECT_FALSE(..) instead of EXPECT_DEATH(..) 
EXPECT_FALSE ( bar ( ) ) 
This code fatals correctly and I can see that foo() was in fact called correctly.

I'm not able to understand why the test behaves as expected using EXPECT_FALSE but in case of EXPECT_DEATH it never calls foo().

Can anyone please help out?

Thanks,
--
Aditya
 
 

--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aditya Tayade

unread,
Mar 12, 2013, 12:16:47 PM3/12/13
to googletes...@googlegroups.com
Using WillRepeatedly instead of WillOnce worked. Thanks a lot!

--
Aditya
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframework+unsub...@googlegroups.com.

Samuel Benzaquen

unread,
Oct 6, 2014, 10:04:24 AM10/6/14
to Karan Bhatnagar, opensource-gtest, Google C++ Testing Framework, adit...@google.com

On Fri, Oct 3, 2014 at 6:41 PM, <karanbh...@google.com> wrote:
Hi
I am facing a similar problem, 

Please see after line 170

I tried multiple combinations of WillRepeatedly and WillOnce with EXPECT_CALL.
It seems that the statement in EXPECT_DEATH(statement) is effective. How to ensure that the statement's effect is visible? I could not quite understand the documentation.

You can't expect the "statement" to kill the program and yet have some other effect.
EXPECT_DEATH() forks the process. In the child process it will run "statement". In the parent process it will not run "statement", but will check that the child died with the expected message.
You can't expect side effects (other than crashing) on either of them.
 

I also tried ON_CALL - the logs seem to reveal that statement in EXPECT_DEATH(statement)  was somewhat effective.
I see warnings telling about return values for mock mehtods. however I do not see any logs from 'statement' itself.

Use .Times(AnyNumber()). This way it won't be an error when the methods are not called in the parent process.
 


Can someone point what I missed?

Is it necessary to write death-tests in some particular style? The EXPECT_DEATH in other TEST_Fs in same file work as expected.

--


Reply all
Reply to author
Forward
0 new messages