With Google Mock 1.7.0, I have a mock object with a method, and I want to expect it to be called, and in this case the mocked method should throw an exception.
ObjectMock object_mock_; EXPECT_CALL(object_mock_, method()) .Times(1) .WillRepeatedly(???);
Is there a Google Mock action that throws an exception? I did not find it in the documentation, yet I doubt that nobody has needed it so far.
Thanks!
So use ON_CALL by default, and only use EXPECT_CALL when you actually intend to verify that the call is made.
In real system, these counterparts belong to the system itself. In the unit tests they are replaced with mocks. Gtest is a framework for unit testing. Gmock is a framework imitating the rest of your system during unit tests.
WillOnce(Return(x))" manipulates the expected value that it is always true. class Turtle { ... virtual int DoSomeMathTurtle(int , int); //Adds two ints together and returns them ... };
In gMock we use the EXPECT_CALL() macro to set an expectation on a mock method. The general syntax is: EXPECT_CALL(mock_object, method(matchers)) . Times(cardinality) .
Just write a simple action that throws an exception:
ACTION(MyThrowException) { throw MyException(); }
And use it as you would do with any standard action:
ObjectMock object_mock_; EXPECT_CALL(object_mock_, method()) .Times(1) .WillRepeatedly(MyThrowException());
There's also a googlemock standard action Throw()
, that supports throwing exceptions as action taken (Note that MyException
must be a copyable class, to get this working!):
ObjectMock object_mock_; EXPECT_CALL(object_mock_, method()) .Times(1) .WillRepeatedly(Throw(MyException()));
Find the full documentation for ACTION
and parametrized ACTION_P<n>
definitions in the GoogleMock CookBook.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With