0

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 (8x8 = 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?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • 1
    I think you should go from 0 to 7 instead of 1 to 8 ;) ... otherwise I don't really see the benefit of mocking vs populating the ArrayBuffer here ? ... would probable make more sense if you created an interface over it – Alois Cochard Aug 23 '11 at 13:30

1 Answers1

1

The main issue here is that the specification is wrong: an ArrayBuffer cannot work as expected in the spec. Thus you must either:

  • Change the expected behavior
  • Change ArrayBuffer for an homemade trait
axel22
  • 32,045
  • 9
  • 125
  • 137
Nicolas
  • 24,509
  • 5
  • 60
  • 66