telling a call to throw an exception

2,652 views
Skip to first unread message

bruce

unread,
Dec 24, 2008, 1:20:20 AM12/24/08
to Google C++ Mocking Framework
Hello,

I would like to do something like:

EXPECT_CALL(obj, func())
.Times(1)
.WillOnce(Throw(MyExceptions()));

Are there facilities in google mock for declaratively directing the
call to throw a particular type of exception?

I looked through the docs and did not see anything.

What is the recommended way to do this?

I was going to make an extension but wanted to check first.

Thanks in advance.

Regards,
Bruce

Zhanyong Wan (λx.x x)

unread,
Dec 24, 2008, 1:32:03 AM12/24/08
to googl...@googlegroups.com
Hi Bruce,
We don't use exceptions much, so we don't have an action for throwing
an exception yet. However, it's easy to add one, similar to how
Return(x) is implemented. Could you log an issue to remind us?

Please follow the examples in the cook book to define a custom action
for now. You can also define a function and use the Invoke() action
to invoke it, if that's easier for you. If you are willing to
contribute a full-fledged Throw() action, all the better!

Thanks,

>
> Thanks in advance.
>
> Regards,
> Bruce
> >
>



--
Zhanyong

Zhanyong Wan (λx.x x)

unread,
Dec 27, 2008, 1:14:14 AM12/27/08
to googl...@googlegroups.com
BTW, using the ACTION_P macro being proposed now, Throw can be defined as

ACTION_P(Throw, ex) { throw ex; }
--
Zhanyong

Bruce Trask

unread,
Dec 27, 2008, 1:55:33 PM12/27/08
to googl...@googlegroups.com
> BTW, using the ACTION_P macro being proposed now, Throw can
> be defined as
>
> ACTION_P(Throw, ex) { throw ex; }

Great. I like the ACTION macro proposal. Much more concise than what I am using now below.

template <typename A>
class ThrowAction {
public:
// Constructs an action that throws the exception 'value'.
explicit ThrowItAction(const A& value) : value_(value) {}

template <typename Result, typename ArgumentTuple>
void Perform(const ArgumentTuple& args) const {
CompileAssertTypesEqual<void, Result>();
throw value_;
}

private:
const A value_;
};

template <typename T>
PolymorphicAction<
internal::ThrowAction<T> >
Throw(const T& x) {
return MakePolymorphicAction(internal::ThrowAction<T>(x));
}

Regards,
Bruce

--- On Sat, 12/27/08, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
Reply all
Reply to author
Forward
0 new messages