Mocking subsequent calls and throwing exceptions

643 views
Skip to first unread message

Mattias Petter Johansson

unread,
Dec 14, 2009, 1:16:54 AM12/14/09
to Moq Discussions
Hello!

I'm currently facing a situation where I am testing code that calls a
(mocked) method twice. On the first call, the method returns some XML
that is handles, and on the second call, it should throws an
exception. I have NO idea how to mock this with Moq! I'm used to
mocking subsequent calls using a queue method:
http://haacked.com/archive/2009/09/29/moq-sequences.aspx

.. but how on earth would I acheive a similiar result when I need it
to throw an exception on the subsequent call?

Thanks.

Mattias Petter Johansson

unread,
Jan 16, 2010, 4:52:00 PM1/16/10
to Moq Discussions
Crap, I assume from everyones silence that this is not possible?

/mattias

On 14 Dec 2009, 07:16, Mattias Petter Johansson

k...@clariusconsulting.net

unread,
Jan 16, 2010, 5:28:42 PM1/16/10
to moq...@googlegroups.com
Just throw from the callback?

/mattias

--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com

Marcos Meli (FileHelpers)

unread,
Jan 18, 2010, 8:56:29 AM1/18/10
to moq...@googlegroups.com
Kzu

I have a simular issue, can u show us an example of a Callback that works different in the first and second call ?

Thanks
Best Regards

Daniel Cazzulino

unread,
Jan 18, 2010, 12:06:25 PM1/18/10
to moq...@googlegroups.com
int calls = 0;
mock.Setup(x => x.Do())
        .Callback(() => calls++)
        .Returns(() => "Called " + calls " times")
        .Callback(() => { if (calls == 2) throw new InvalidOperationException(); });

:)

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

Marcos Meli (FileHelpers)

unread,
Jan 18, 2010, 3:03:09 PM1/18/10
to moq...@googlegroups.com
Thanks kzu :) Pretty simple

It is a cool example for the docs, I known that the intellisense show up the alternatives but I didn't catched this one.

Cheers
Marcos

Tomas Johansson

unread,
Feb 28, 2010, 10:54:48 AM2/28/10
to Moq Discussions
What is the most convenient way of specifying a sequence of return
value from a certain method ?

For example, assume that you would like to implement a Test Stub for
returning some random integers within a certain interval (e.g. between
20 and 70 if you would want to use the values for some random ages for
adult people).

I am used to the Java framework Mockito, and with that framework you
could do it easily like this, if you want the first invocation to
return 37, the second to return 65 and so on:
Randomizer randomizer = Mockito.mock(Randomizer.class);
Mockito.when(randomizer.getRandomInteger(20,
70)).thenReturn(37).thenReturn(65).thenReturn(28).thenReturn(56).thenReturn(23);
//Alternatively, you can do it even easier like this with Mockito:
Mockito.when(randomizer.getRandomInteger(20, 70)).thenReturn(37, 65,
28, 56, 23);

I have not been able to find any similarly convenient way of doing the
same thing in Moq.
By experimenting with the code in this thread which I now replied to,
I can indeed do the same thing but not in a similarly easy way.
As far I can tell, it is necessary to use some "switch/if statement"
code, or some table lookup.
For example, using the table lookup alternative, you might use a
Dictionary as below:

var randomizer = new Mock<Randomizer>();

var expectedRandomIntegersWhenMinAndMaxValuesAre20And70 = new
Dictionary<int, int>();
expectedRandomIntegersWhenMinAndMaxValuesAre20And70[1] = 37; // first
invocation of 'randomizer.GetRandomInteger(20, 70)' should return 37
expectedRandomIntegersWhenMinAndMaxValuesAre20And70[2] = 65; // second
invocation of 'randomizer.GetRandomInteger(20, 70)' should return 65
expectedRandomIntegersWhenMinAndMaxValuesAre20And70[3] = 28; // ...
expectedRandomIntegersWhenMinAndMaxValuesAre20And70[4] = 56;
expectedRandomIntegersWhenMinAndMaxValuesAre20And70[5] = 23;

int callCounter = 0;
randomizer.Setup( x => x.GetRandomInteger(20, 70) )
.Callback(() => callCounter++)
.Returns( () =>
expectedRandomIntegersWhenMinAndMaxValuesAre20And70[callCounter]);

Though, I would like to find a more convenient syntax, similar to
Mockito as in the example above, if it is possible ?

/ Tomas

Tomas Johansson

unread,
Feb 28, 2010, 10:56:57 AM2/28/10
to Moq Discussions

/ Tomas

Daniel Cazzulino

unread,
Feb 28, 2010, 9:34:08 PM2/28/10
to moq...@googlegroups.com
Thanks for bringing this up. The mockito sample is helpful in influencing our implementation in this area.

This is somehing we'll be improving in v4

/from crappy S60 mobile OS

Tomas Johansson

unread,
Mar 1, 2010, 6:01:53 PM3/1/10
to Moq Discussions
Okay Daniel, that sounds good.
Thank you.
/ Tomas


On 1 mar, 03:34, "Daniel Cazzulino" <k...@clariusconsulting.net>
wrote:

Reply all
Reply to author
Forward
0 new messages