Skip to content

Instantly share code, notes, and snippets.

@sam
Created March 13, 2013 20:05
Show Gist options
  • Save sam/5155653 to your computer and use it in GitHub Desktop.
Save sam/5155653 to your computer and use it in GitHub Desktop.
ScalaTest Helpers for testing Futures. Scala, Currying, Pattern Matching and Functional Programming is awesome.
def whenReady[A](result:Future[A], timeout:Duration = 1 second)(expectation: A => Unit) = {
expectation(Await.result(result, timeout))
}
def tryWhenReady[A](result:Future[Try[A]], timeout:Duration = 1 second)
(failure:Throwable => Unit)
(expectation: A => Unit) = {
Await.result(result, timeout) match {
case Failure(e) => failure(e)
case Success(result:A) => expectation(result)
}
}
"deleting a document" should {
"be successful" in {
tryWhenReady(User.create(scott))(fail) { userDoc =>
tryWhenReady(User.delete(userDoc))(fail) { _ must be ('ok) }
}
whenReady(User.create(scott)) {
case Failure(e) => fail(e)
case Success(user) =>
whenReady(User.delete(user)) {
case Failure(e) => fail(e)
case Success(result) => result must be ('ok)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment