I’m using Mockito as a part of Specs in scala code and I’ve stumbled upon the following task:
Given an ArrayBuffer that emulates a chess board (8×8 = 64 cells). If we querying ArrayBuffer for cell that doesn’t exist (has number more than 63 or less than 0) we should receive None. Otherwise we returning Some(0) (in almost all cases) or Some(1) (just in few specified cells).
Right now I’m thinking about spies and something that starts like:
val spiedArray = spy(new ArrayBuffer[Int])
for (x <- 1 to 8; y <- 1 to 8) {
doReturn(Some(0)).when(spiedArray).apply(x * y-1)
}
And then explicitly respecify cells with Some(1).
But how about out-of-bound cells that should return None?
Is there a simplest and natural way to achieve that mocking?
The main issue here is that the specification is wrong: an
ArrayBuffercannot work as expected in the spec. Thus you must either:ArrayBufferfor an homemade trait