开发者

Mockito different range expectations

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 开发者_StackOverflowquerying 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 ArrayBuffer cannot work as expected in the spec. Thus you must either:

  • Change the expected behavior
  • Change ArrayBuffer for an homemade trait
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜