IUnknown
IUnknown

Reputation: 2636

mocking method that returns Try[T] using scalamock

I know this is probably missing something very obvious but I can't seem to mock out a method that returns Try[T]

Simplified example:

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)
}

How do I return a failed Try[T] from the mocked method?

Upvotes: 1

Views: 1621

Answers (1)

Noah
Noah

Reputation: 13959

The T is bound to User in your example, so return a User failure:

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

Upvotes: 5

Related Questions