【问题标题】:mocking method that returns Try[T] using scalamock使用 scalamock 返回 Try[T] 的模拟方法
【发布时间】:2015-01-17 13:30:35
【问题描述】:

我知道这可能遗漏了一些非常明显的东西,但我似乎无法模拟出返回 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]?

【问题讨论】:

    标签: scala unit-testing mocking scalatest


    【解决方案1】:

    在您的示例中,T 绑定到 User,因此返回 User 失败:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2015-10-26
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多