簡體   English   中英

使用scalamock返回Try [T]的模擬方法

[英]mocking method that returns Try[T] using scalamock

我知道這可能缺少非常明顯的東西,但是我似乎無法模擬出返回Try [T]的方法

簡化示例:

case class User (first: String, last: String)

// ...

// in Repository class:
  def update[T](id: Int): Try[T] = { ... }

// ...

// in scalatest test using scalamock:
test("a bad id should return a failed update") {
  val mockRepo = mock[Repository]
  (mockRepo.update[User] _).expects(13).returns(???)

  val result = mockRepo.update[User](13)
  result.isSuccess should be (false)
}

如何從模擬方法返回失敗的Try [T]?

在您的示例中, TUser綁定,因此返回User失敗:

(mockRepo.update[User] _).expects(13).returns(Failure[User](new Exception("Failed!")))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM