Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:05:48+00:00 2026-06-12T02:05:48+00:00

I have the following method: def save(entity: A): Either[List[Error],A] + {…. which I want

  • 0

I have the following method:

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

which I want to test using specs2

I want to test for the existence of a specific error when a required field is not specified, like this:

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’d like to define my own matcher to make it less verbose, like this:

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

I had a look at the documentation (http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#Matchers) but I can’t figure out how to define a custom matcher like haveError.like

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:05:49+00:00Added an answer on June 12, 2026 at 2:05 am

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

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

    Notice that the matcher is defined on Either[List[T], _] everywhere.

    I’m also wondering about the failure messages that are returned in case you don’t find the expected error message, they might not be very explicit when the partial function fails.

    So you may want to aim for using a contain matcher. 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 one (using an implicit) and the beLike matcher, to get maximum reusability of 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)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method where I want to test the event.status property only
I have the following method in my model def reset_review_status needs_review = true save
I have the following method: def call_http_service(url, url_params) begin conn = create_connection(url) resp =
In my messages_controller I have the following private method: def find_message_or_404(slug) message = user.messages.find_by_slug(slug)
I have the following application_controller method: def current_account @current_account ||= Account.find_by_subdomain(request.subdomain) end Should I
I currently have the following controller method in a Rails app: def index @entries
I have the following method, which takes in the name of a file as
I have the following piece of code overriding the save method of a model:
Suppose we have following code defined in tester.py class Tester( object ): def method(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.