【发布时间】:2016-06-30 21:19:06
【问题描述】:
我想在测试中重复使用模拟声明(如果可能的话)。 这是一个使用 ScalaTest 和 Mockito 的最小非工作示例。我期待第一次测试中的值是 yes,但我得到的是other值。
似乎最新的Mockito.when 是适用于所有测试条款的那个。
有没有办法避免在每个in 子句中声明模拟?
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
class ReuseMocksSpec extends WordSpec with Matchers with MockitoSugar {
"A test" when {
val service = mock[Service]
"sharing mocks among tests" should {
when(service.getVal).thenReturn("yes")
"get yes value" in {
service.getVal should be("yes")
}
}
"sharing mocks among other tests" should {
when(service.getVal).thenReturn("other")
"get other value" in {
service.getVal should be("other")
}
}
}
trait Service {
def getVal: String
}
}
【问题讨论】: