나는 예를 들어, 농축을 입력 부착 될 것스칼라 테스트
나는이object Algorithm{
def apply(rand: Random) = {
import MyImplicits._
rand.nextInt.complexCalculation + 1
}
}
같은 코드에 사용하지만 지금은 분리 및 단위 테스트 알고리즘 방법
object MyImplicits{
implicit class RichInt(i: Int){
def complexCalculation: Int = i * 200
}
}
? 특히,이 같은 complexCalculation
의 실현하는 것이, 무언가를 조롱하고 싶습니다 :
class MyAlgorithmTest extends FreeSpec with MockitoSugar{
import org.mockito.Mockito.when
"MyApgorithm" {
"Delegates complex calculation" in {
val mockRandom = mock[Random]
when(mockRandom.nextInt()).thenReturn(1)
// This wouldn't work, but is the kind of thing I'm looking for
//when(1.complexCalculation).thenReturn(2)
val expected = 1 * 2 + 1
val result = MyAlgorithm(mockRandom)
assert(result === expected)
}
}
}
귀하의 문제는 무엇입니까? 테스트하는 동안 임의의 int 객체가 RichInt 객체로 변환되는지 확인하려고합니까? 아니면 모르는 무작위 값에 대한 어설 션을 사용하는 방법을 모르는 것입니까? – Samar
@Pengin은'complexCalculation' 메소드를 조롱하길 원하지만'RichInt' 오브젝트의 생성이 암묵적으로 이루어지기 때문에'RichInt' 오브젝트에'when' 절을 지정할 방법이 없습니다. – virsox