Force sequence EXPECT_CALL

I am trying to test two serial write + read calls ports. The first should block on read for 101 ms and thus cause a timeout error. The second should work fine. Based on the Google mock guide , the InSequence variable should provide this. Here is the code:

  { // This variable ensures the sequence order InSequence s; EXPECT_CALL(serial_, Write(_, _)) .With(ElementsAreArray(command1_)) .WillOnce(Invoke(Write)); EXPECT_CALL(serial_, Read(_, _, _)) .WillOnce(Invoke(TimeoutRead)); EXPECT_CALL(serial_, Write(_, _)) .With(ElementsAreArray(command2_)) .WillOnce(Invoke(Write)); EXPECT_CALL(serial_, Read(_, _, _)) .Times(1) .WillOnce(DoAll(SetArrayArgument<0>(response_begin, response_end), SetArgumentPointee<2>(response_.size()), Return(true))); } 

TimeoutRead is just a function that makes the current thread sleep for 101 ms. However, I keep getting this runtime error:

 [ RUN ] MasterTest.LostReply /host/Users/blah/work/bitbucket/master_test.cc:631: Failure Mock function called more times than expected - returning default value. Function call: Read(0x5652c3f31120 pointing to "\xFF\xFF", 4096, 0x7ffda19d2960) Returns: false Expected: to be called once Actual: called twice - over-saturated and active 

GTest seems to be trying to call the last EXPECT_CALL intended for the second read .

Is there a way to force this call sequence?


[THIS IS A TEMPORARY PICTURE OF THIS PROCESS TO BE PROVIDED ANSWERED]
This is SSCCE. I expect you to tune in correctly to get the error you mention in your question. The code below works and passes:

 class Serial_ { public: MOCK_METHOD2(Write, void(int* elems, size_t n)); MOCK_METHOD3(Read, bool(int* elems, int dummy, size_t* n)); }; int command1_[] = {1,2,3}; int command2_[] = {4,5}; std::array<int, 4> response_{{ 6, 7, 8, 9 }}; void testScenario(Serial_& serial_) { int dummy_arr[100]; size_t dummy_size; serial_.Write(command1_, 3); serial_.Read(dummy_arr, 123, &dummy_size); serial_.Write(command2_, 2); serial_.Read(dummy_arr, 123, &dummy_size); } bool TimeoutRead(int* elems, int dummy, size_t* n) { return true; } void Write(int* elems, size_t n) {} TEST(A,A) { Serial_ serial_; // This variable ensures the sequence order InSequence s; EXPECT_CALL(serial_, Write(_, _)) .With(ElementsAreArray(command1_)) .WillOnce(Invoke(Write)); EXPECT_CALL(serial_, Read(_, _, _)) .WillOnce(Invoke(TimeoutRead)); EXPECT_CALL(serial_, Write(_, _)) .With(ElementsAreArray(command2_)) .WillOnce(Invoke(Write)); EXPECT_CALL(serial_, Read(_, _, _)) .Times(1) .WillOnce(DoAll(SetArrayArgument<0>(response_.begin(), response_.end()), SetArgumentPointee<2>(response_.size()), Return(true))); testScenario(serial_); } 
+7
c ++ googletest googlemock gmock
source share

No one has answered this question yet.

See related questions:

950
Undefined behavior and sequence points
2
EXPECT_CALL no errors in the Google test
2
Defining method behavior through EXPECT_CALL vs in body
one
EXPECT_CALL in mock template
one
Object argument to retrieve EXPECT_CALL
one
Why do I get different results when gmock EXPECT_CALL is called with Times (n) for stack objects and heap objects?
one
GTest test error "EXPECT_CALL"
0
Cannot improve gMock performance by EXPECT_CALL
0
GMock does not match EXPECT_CALL
0
Gmock detected an error when the actual number of function calls does not match EXPECT_CALL

All Articles