开发者

Mockito different range expectations

开发者 https://www.devze.com 2023-03-29 21:44 出处:网络
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 Ar

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

精彩评论

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

关注公众号