Scala: how to implement this matches in specs2

I have the following method:

def save(entity: A): Either[List[Error],A] + {.... 

which I want to check using specs2

I want to check for a specific error when the required field is not specified, for example:

 val noNickname = User( nickname = "", name = "new name", ) noNickname.save must beLeft.like { case errors => { atLeastOnceWhen(errors) { case error => { error.errorCode must equalTo(Error.REQUIRED) error.field must equalTo("nickname") } } } } 

It works fine, but I would like to define my own helper to make it less verbose, for example:

 noNickname.save must haveError.like { case error => { error.errorCode must equalTo(Error.REQUIRED) error.field must equalTo("nickname") } } } 

I looked through the documentation (http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#Matchers), but I cannot figure out how to define a custom connector like haveError.like

+4
source share
1 answer

Here is your code with a few changes to compile it:

 case class Error(errorCode: String, field: String) def save[A](entity: A): Either[List[Error],A] = Left(List(Error("REQUIRED", "nickname"))) case class User(nickname: String, name: String) val noNickname = User(nickname = "", name = "new name") "save noNickName" >> { save(noNickname) must haveError.like { case error => { error.errorCode must equalTo("REQUIRED") error.field must equalTo("nickname") } } } def haveError[T] = new ErrorMatcher[T] class ErrorMatcher[T] extends Matcher[Either[List[T], _]] { def apply[S <: Either[List[T], _]](value: Expectable[S]) = result(value.value.left.toOption.isDefined, value.description + " is Left", value.description + " is not Left", value) def like[U](f: PartialFunction[T, MatchResult[U]]) = this and partialMatcher(f) private def partialMatcher[U](f: PartialFunction[T, MatchResult[U]]) = new Matcher[Either[List[T], _]] { def apply[S <: Either[List[T], _]](value: Expectable[S]) = { // get should always work here because it comes after the "and" val errors = value.value.left.toOption.get val res = atLeastOnceWhen[T, U](errors)(f) result(res.isSuccess, value.description+" is Left[T] and "+res.message, value.description+" is Left[T] but "+res.message, value) } } } 

Note that a match is defined on Either[List[T], _] everywhere.

I am also interested to know about the error messages that are returned if you did not find the expected error message, they may not be very obvious if the incomplete function does not work.

So you might want to use content matching. Like this:

 "save noNickName" >> { save(noNickname) must haveError.containing(Error("REQUIRED", "nickname")) } // I'm reusing the beLeft matcher here def haveError[T]: Matcher[Either[List[T], _]] = beLeft // and using an implicit conversion to extend it implicit def toErrorListMatcher[T](m: Matcher[Either[List[T], _]]): ErrorListMatcher[T] = new ErrorListMatcher[T](m) class ErrorListMatcher[T](m: Matcher[Either[List[T], _]]) { def containing(t: T) = // the 'contain' matcher is adapted to take in an // Either[List[T], _] and work on its left part m and contain(t) ^^ ((e: Either[List[T], _]) => e.left.toOption.get) } 

[Update]

The first solution (using atLeastOnceWhen and a partial function) can be combined with the second (using implicit) and beLike matches to get the most reuse of the existing specs2 code:

 def haveError[T]: Matcher[Either[List[T], _] = beLeft implicit def toErrorListMatcher[T](m: Matcher[Either[List[T], _]]): ErrorListMatcher[T] = new ErrorListMatcher[T](m) class ErrorListMatcher[T](m: Matcher[Either[List[T], _]]) { // beLike checks one element // beLike.atLeastOnce transforms that matcher on a // matcher on a sequence of elements def like[S](f: PartialFunction[T, MatchResult[S]]) = { m and beLike(f).atLeastOnce ^^ ((e: Either[List[T], _]) => e.left.toOption.get) } 
+7
source

Source: https://habr.com/ru/post/1436913/


All Articles