如何在Scala中模拟整数

时间:2014-07-29 21:28:29

标签: scala integer tdd

目的是模拟在Scala中生成随机整数的函数。函数返回整数时发生错误value is not a member of int

测试

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._

class NumberSequencesTests extends FunSuite with BeforeAndAfter with MockitoSugar {
  test("randomInteger") {
    when(NumberSequences.numbersequence(5).thenReturn(5))
  }
}

主要

object NumberSequences {
  def randomInteger(a: Int) : Int = {
    scala.util.Random.nextInt(a) + 1
  }
}

输出:

[error] C:\scala\numbersequences\scala\NumberSequencesTest.scala:20: value the
nReturn is not a member of Int
[error]         when(NumberSequences.numbersequence(5).thenReturn(5))
[error]                                                ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 1 s, completed Jul 29, 2014 11:19:30 PM

1 个答案:

答案 0 :(得分:3)

替换

when(NumberSequences.numbersequence(5).thenReturn(5))

when(NumberSequences.numbersequence(5)).thenReturn(5)