most idiomatic way to structure tests and validation

9 views
Skip to first unread message

Tim

unread,
Dec 24, 2014, 7:07:31 AM12/24/14
to specs2...@googlegroups.com
Hi
the code below, where fsm.process returns a scalaz Validation used to work and is reasonably clear.
However, I now have changed fsm.process to return a Future[Validation[...]] and I have to restructure quite a lot of little tests

What's the neatest and most specs2 idiomatic way to structure it? I saw something had been done about supporting futures directly on a ticket but I wasn't clear how it would apply to me.
Thanks

Tim

fsm.process(endBreak, initial).map( _  match {
case Success(request) =>
request.state === Final
request.data === NoData
request.stopReason === Some(endBreak)
case Failure(x) => x === "junk"
})

etorreborre

unread,
Dec 24, 2014, 7:51:24 AM12/24/14
to specs2...@googlegroups.com
Hi Tim,

When you have a Matcher[T] you can transform it into a Matcher[Future[T]] by adding .await.

So you should be able to write:

fsm.process(endBreak, initial) must aCorrectRequest(Final, NoData, endBreak).await

where 

def aCorrectRequest(state: State, data: Data, reason: Reason): Matcher[Validation[String, Request]] = (validation: Validation[String, Request]) => {
    validation match { 
       case Success(request) => 
         request.state === state
         request.data === data
         request.stopReason === Some(reason)
       case Failure(x) => x === "junk"
    }
}

I haven't tried to compile that precise code so please tell me if that is not working for you.

Eric.
Reply all
Reply to author
Forward
0 new messages