/mattias
On 14 Dec 2009, 07:16, Mattias Petter Johansson
/mattias
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
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
This is somehing we'll be improving in v4
/from crappy S60 mobile OS
On 1 mar, 03:34, "Daniel Cazzulino" <k...@clariusconsulting.net>
wrote: